ES6 异步操作和Async函数(18)
时间:2021-06-24 13:06 来源:汽车之家 作者:阿虎 点击:次
awaitawaitPromiseyieldnextawait file.readLine()next()valueGenerator Generator for await ( const line of readLines(filePath)) { console .log(line);} Generatorfor await...of async function * prefixLines ( asyncIterable ) { for await ( const line of asyncIterable) { yield + line; }} yieldPromise async function * asyncGenerator () { console .log( Start ); const result = await doSomethingAsync(); // (A) yield Result: + result; // (B) console .log( Done );} nextByieldPromisePromiseAawaitPromiseAPromiseresolveresultBPromiseresolveResult + resultAPromiseBPromiseresolve GeneratorPromiserejectcatch async function * asyncGenerator () { throw new Error ( Problem! );}asyncGenerator().next().catch(err = console .log(err)); // Error: Problem! asyncPromiseGeneratorIteratorasyncGeneratorfor await...ofGenerator async function takeAsync ( asyncIterable, b ]); const [{value: v1}。
{}]; let promises = docs.map((doc) = db.post(doc)); let results = await Promise .all(promises); console .log(results);} // async function dbFuc ( db ) { let docs = [{}, callback ) { var sum = a + b; callback(sum); callback(sum);} var ft = thunkify(f); var print = console .log.bind( console );ft( 1 , done: true } Generatorg Generatorgnextyieldx + 2 nextGeneratornextvaluedonevalueyielddoneGenerator Generator Generator (责任编辑:admin) |