在/sf/answers/1306102941/中是如何使用内置加密库和流计算文件的md5的示例.
var fs = require('fs');
var crypto = require('crypto');
// the file you want to get the hash
var fd = fs.createReadStream('/some/file/name.txt');
var hash = crypto.createHash('sha1');
hash.setEncoding('hex');
fd.on('end', function() {
hash.end();
console.log(hash.read()); // the desired sha1sum
});
// read all file and pipe it (write it) to the hash object
fd.pipe(hash);
Run Code Online (Sandbox Code Playgroud)
但是有可能将其转换为使用ES8 async/await而不是如上所述使用回调,但是仍然保持使用流的效率吗?
我对目前关于将async函数和关键字添加await到下一个EcmaScript的讨论感到困惑.
我不明白为什么有必要在async关键字之前使用function关键字.
从我的观点来看,await关键字等待发电机或承诺的结果做一个函数的return应该是足够的.
await应该在普通函数和生成器函数中简单可用,无需额外的async标记.
如果我需要创建一个函数作为结果应该可用await,我只需使用一个promise.
我的理由是这个很好的解释,下面的例子来自:
async function setupNewUser(name) {
var invitations,
newUser = await createUser(name),
friends = await getFacebookFriends(name);
if (friends) {
invitations = await inviteFacebookFriends(friends);
}
// some more logic
}
Run Code Online (Sandbox Code Playgroud)
它也可以作为普通函数完成,如果函数的执行将等待完成孔函数,直到满足所有等待.
function setupNewUser(name) {
var invitations,
newUser = await createUser(name),
friends = await getFacebookFriends(name);
if (friends) {
invitations = await inviteFacebookFriends(friends);
}
// return because createUser() and getFacebookFriends() …Run Code Online (Sandbox Code Playgroud) 我用这段代码得到编译时错误:
const someFunction = async (myArray) => {
return myArray.map(myValue => {
return {
id: "my_id",
myValue: await service.getByValue(myValue);
}
});
};
Run Code Online (Sandbox Code Playgroud)
错误信息是:
等待是一个保留字
为什么我不能这样使用它?
我也尝试了另一种方式,但它给了我同样的错误:
const someFunction = async (myArray) => {
return myArray.map(myValue => {
const myNewValue = await service.getByValue(myValue);
return {
id: "my_id",
myValue: myNewValue
}
});
};
Run Code Online (Sandbox Code Playgroud) 以下
我试图在React.js应用程序中使用Async/Await向我的服务器发出一个简单的get请求.服务器加载一个/data看起来像这样的简单JSON
JSON
{
id: 1,
name: "Aditya"
}
Run Code Online (Sandbox Code Playgroud)
我可以使用简单的jquery ajax get方法将数据提供给我的React App.但是,我想利用axios库和Async/Await来遵循ES7标准.我当前的代码如下所示:
class App extends React.Component{
async getData(){
const res = await axios('/data');
console.log(res.json());
}
render(){
return(
<div>
{this.getData()}
</div>
);
}
}
Run Code Online (Sandbox Code Playgroud)
使用这种方法我收到以下错误:
对象作为React子对象无效(找到:[object Promise]).如果您要渲染子集合,请使用数组.
我没有正确实施吗?
action.js
export function getLoginStatus() {
return async(dispatch) => {
let token = await getOAuthToken();
let success = await verifyToken(token);
if (success == true) {
dispatch(loginStatus(success));
} else {
console.log("Success: False");
console.log("Token mismatch");
}
return success;
}
}
Run Code Online (Sandbox Code Playgroud)
component.js
componentDidMount() {
this.props.dispatch(splashAction.getLoginStatus())
.then((success) => {
if (success == true) {
Actions.counter()
} else {
console.log("Login not successfull");
}
});
}
Run Code Online (Sandbox Code Playgroud)
但是,当我用async/await编写component.js代码时,我得到此错误:
Possible Unhandled Promise Rejection (id: 0): undefined is not a function (evaluating 'this.props.dispatch(splashAction.getLoginStatus())')
component.js
async componentDidMount() {
let success = …Run Code Online (Sandbox Code Playgroud) reactjs react-native redux-thunk react-redux ecmascript-2017
我一直在使用TypeScript和本机Promise在SPA中使用该功能,我注意到即使我将长时间运行的函数重构为返回promise的异步函数,UI仍然没有响应.
所以我的问题是:
新的异步/等待功能究竟如何帮助避免阻止浏览器中的UI?在使用async/await实际获得响应式UI时,是否需要采取任何特殊的额外步骤?
有人可以创建一个小提琴来演示async/await如何帮助使UI响应?
async/await如何与先前的异步功能(如setTimeout和XmlHttpRequest)相关联?
javascript user-interface asynchronous async-await ecmascript-2017
我正在尝试异步/等待功能.我有这样的代码模仿请求:
const getJSON = async () => {
const request = () => new Promise((resolve, reject) => (
setTimeout(() => resolve({ foo: 'bar'}), 2000)
));
const json = await request();
return json;
}
Run Code Online (Sandbox Code Playgroud)
当我以这种方式使用代码时
console.log(getJSON()); // returns Promise
Run Code Online (Sandbox Code Playgroud)
它返回一个Promise
但是当我调用这行代码时
getJSON().then(json => console.log(json)); // prints { foo: 'bar' }
Run Code Online (Sandbox Code Playgroud)
它按预期打印json
可以只使用像这样的代码console.log(getJSON())吗?我不明白什么?
感谢firebase 添加了对promises的支持,有没有办法在async函数内运行如下的查询?:
const eventref = this.db.ref('cats/whiskers');
const value = await eventref.once('value')
Run Code Online (Sandbox Code Playgroud)
运行上面的命令会返回一个承诺value,我希望得到存储在的json blob cats/whiskers.
我正在努力学习async-await.在这段代码中 -
const myFun = () => {
let state = false;
setTimeout(() => {state = true}, 2000);
return new Promise((resolve, reject) => {
setTimeout(() => {
if(state) {
resolve('State is true');
} else {
reject('State is false');
}
}, 3000);
});
}
const getResult = async () => {
return await myFun();
}
console.log(getResult());
Run Code Online (Sandbox Code Playgroud)
为什么我得到输出 -
Promise { <pending> }
Run Code Online (Sandbox Code Playgroud)
而不是一些价值?getResult()功能等待myFun()功能不应该解决它的承诺值吗?
查看有关ECMAScript2015 (ES6) 浏览器支持的数据相当容易,但我发现很难为以下所有 ES 版本(ES7 到 ES10)提供一个同样清晰的表格。
Mozilla在他们的网站上有一些信息,可以看到完全支持 ES7 和 ES8,ES9 仍然存在一些问题,最新版本支持 ES10。
我也可以猜到 IE11 在 ES5 之后就再也没有进步了。
我没有找到其他浏览器的任何东西,只是到处都是一些被盗的信息。
有谁知道目前的支持水平是多少?
ecmascript-2017 ×10
javascript ×8
async-await ×5
asynchronous ×4
node.js ×3
reactjs ×2
ecmascript-6 ×1
firebase ×1
json ×1
promise ×1
react-native ×1
react-redux ×1
redux-thunk ×1