美文网首页
nodejs中async函数与callbacks

nodejs中async函数与callbacks

作者: 洵_BlackBYR | 来源:发表于2017-11-24 20:52 被阅读9次

One limitation of async functions is that await only affects the directly surrounding async function. Therefore, an async function can’t await in a callback (however, callbacks can be async functions themselves, as we’ll see later on). That makes callback-based utility functions and methods tricky to use. Examples include the Array methods map() and forEach().

Let’s start with the Array method map(). In the following code, we want to download the files pointed to by an Array of URLs and return them in an Array.

async function downloadContent(urls) {
    return urls.map(url => {
        // Wrong syntax!
        const content = await httpGet(url);
        return content;
    });
}

This does not work, because await is syntactically illegal inside normal arrow functions. How about using an async arrow function, then?

async function downloadContent(urls) {
    return urls.map(async (url) => {
        const content = await httpGet(url);
        return content;
    });
}

There are two issues with this code:

  • The result is now an Array of Promises, not an Array of strings.
  • The work performed by the callbacks isn’t finished once map() is finished, because await only pauses the surrounding arrow function and httpGet() is resolved asynchronously. That means you can’t use await to wait until downloadContent() is finished.

We can fix both issues via Promise.all(), which converts an Array of Promises to a Promise for an Array (with the values fulfilled by the Promises):

async function downloadContent(urls) {
    const promiseArray = urls.map(async (url) => {
        const content = await httpGet(url);
        return content;
    });
    return await Promise.all(promiseArray);
}

The callback for map() doesn’t do much with the result of httpGet(), it only forwards it. Therefore, we don’t need an async arrow function here, a normal arrow function will do:

async function downloadContent(urls) {
    const promiseArray = urls.map(
        url => httpGet(url));
    return await Promise.all(promiseArray);
}

There is one small improvement that we still can make: This async function is slightly inefficient – it first unwraps the result of Promise.all() via await, before wrapping it again via return. Given that return doesn’t wrap Promises, we can return the result of Promise.all() directly:

async function downloadContent(urls) {
    const promiseArray = urls.map(
        url => httpGet(url));
    return Promise.all(promiseArray);
}

相关文章

  • nodejs中async函数与callbacks

    One limitation of async functions is that await only affe...

  • keras训练早停法EarlyStopping

    EarlyStopping的使用与技巧一般是在model.fit函数中调用callbacks,fit函数中有一个参...

  • Nodejs - Koa2返回结果过程可以为异步

    Koa2中返回结果函数可包含异步过程,如果使用nodejs版本大于8,可以使用Async/Await;如果node...

  • (JavaScript) 异步JavaScript

    一) Callbacks 异步callbacks其实就是函数,只不过是作为参数传递给那些在后台执行的其他函数. 当...

  • D005+技术|jQuery-$.Callbacks()实现原理

    $.Callbacks用于管理函数队列,通过add添加处理函数到队列中,通过fire去执行这些处理函数。 本节向大...

  • async和await的使用

    async函数 什么是async函数? async函数是使用async关键字声明的函数。 mdn文档:https:...

  • React组件中绑定回调

    原文:Binding callbacks in React components 在组件中给事件绑定处理函数是很常...

  • async函数

    1.async语法 async函数自带执行器。async函数的执行,与普通函数一模一样 await表示紧跟在后面的...

  • Async的使用

    Async的使用 Async函数已写入ES7标准中,通过async函数可以更友好直观的写异步代码。 在实际工作中,...

  • async函数

    一、async函数与generator函数的关系   她同样适用用来处理异步操作,并且async函数是对gener...

网友评论

      本文标题:nodejs中async函数与callbacks

      本文链接:https://www.haomeiwen.com/subject/pmyfbxtx.html