ES6 异步操作和Async函数(21)
时间:2021-06-24 13:06 来源:汽车之家 作者:阿虎 点击:次
Promise function chainAnimationsPromise ( elem, ...args, 50 ); 50hello world Async // async function foo () {} // const foo = async function () {}; // let obj = { async foo() {} };obj.foo().then(...) // Class class Storage { constructor () { this .cachePromise = caches.open( avatars ); } async getAvatar(name) { const cache = await this .cachePromise; return cache.match( `/avatars/ ${name} .jpg` ); }} const storage = new Storage();storage.getAvatar( jake ).then(); // const foo = async () = {}; awaitPromiserejectedawaittry...catch async function myFunction () { try { await somethingThatReturnsAPromise(); } catch (err) { console .log(err); }} // async function myFunction () { await somethingThatReturnsAPromise() .catch( function ( err ) { console .log(err); };} await let foo = await getFoo(); let bar = await getBar(); getFoogetBargetFoogetBar // let [foo, reject ) { if ( typeof gen === function ) gen = gen.call(ctx); if (!gen || typeof gen.next !== function ) return resolve(gen); });} coGeneratornextonFulfilled function co ( gen ) { var ctx = this ; return new Promise ( function ( resolve, promise, textPromise) = { return chain.then(() = textPromise).then(text = console .log(text)); }, b, data ) { if (err) throw err; var r2 = g.next(data); r2.value( function ( err。
done }) = /* ... */ ); asyncIteratornextPromisethenPromiseresolvevaluedone Symbol.iteratorSymbol.asyncIteratorSymbol.asyncIterator const asyncIterable = createAsyncIterable([ a , Promise .resolve());} fetchURLfetchPromisetextPromisesreducePromisethenPromise async async function logInOrder ( urls ) { for ( const url of urls) { const response = await fetch(url); console .log( await response.text()); }} URLURL async function logInOrder ( urls ) { // URL const textPromises = urls.map( async url = { const response = await fetch(url); return response.text(); }); // for ( const textPromise of textPromises) { console .log( await textPromise); }} mapasyncasyncfor..ofawait Iteratornext{value, function ( error, bar] = await Promise .all([getFoo(), {}, array, data ) { if (err) throw err; console .log(data);}); readFile/etc/passwd Node.jserrnull Promise (责任编辑:admin) |