如何使此代码按顺序运行

如何使此代码按顺序运行

本文介绍了节点异步循环 - 如何使此代码按顺序运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有几个关于此的帖子,但根据我发现的那些,这应该可以正常工作。

I know there are several posts about this, but according to those I've found, this should work correctly.

我想在一个http请求中循环,我不希望循环迭代,直到请求回调被触发。我正在使用async库:

I want to make an http request in a loop and I do not want the loop to iterate until the request callback has been fired. I'm using the async library like so:

const async = require("async");
const request = require("request");

let data = [
    "Larry",
    "Curly",
    "Moe"
];

async.forEachOf(data, (result, idx, callback) => {
    console.log("Loop iterated", idx);
    let fullUri = "https://jsonplaceholder.typicode.com/posts";
    request({
        url: fullUri
    },
    (err, res, body) => {
        console.log("Request callback fired...");
        if (err || res.statusCode !== 200) return callback(err);
        console.log(result);
        callback();
    });
});

我看到的是:

Loop iterated 0
Loop iterated 1
Loop iterated 2
Request callback fired...
Curly
Request callback fired...
Larry
Request callback fired...
Moe

什么我需要看到的是:

Loop iterated 0
Request callback fired...
Curly
Loop iterated 1
Request callback fired...
Larry
Loop iterated 2
Request callback fired...
Moe

另外,如果有一种内置的方法来做同样的事情(async / await?Promise?)并且可以删除异步库,那会更好。

Also, if there's a built-in way to do the same thing (async/await? Promise?) and the async library could be removed, that'd be even better.

我看到一些聪明的递归示例,但当我把它用于更复杂的情况时(例如,每个循环多次请求调用等)我觉得这种方法难以理解,并且不那么可读。

I've seen some examples of recursion out there that are clever, but when I put this to use in a much more complex situation (e.g. multiple request calls per-loop, etc.) I feel like that approach is hard to follow, and isn't as readable.

推荐答案

你可以完全抛弃 async 并轻松地去 async / await

You can ditch async altogether and go for async/await quite easily.

请求转入承诺,这样就可以等待了。

Just turn request into a Promise so you can await on it.

更好的是,只需使用已使用本机Promises包装请求。

Better yet just use request-promise-native that already wraps request using native Promises.

从此开始这是一个扣篮, async / await

From then on it's a slam dunk with async/await:

const rp = require('request-promise-native')

const users = [1, 2, 3, 4]
const results = []

for (const idUser of users) {
  const result = await rp('http://foo.com/users/' + idUser)

  results.push(result)
}



并行示例



现在,问题是上面的解决方案是它很慢 - 请求串行运行。这在大多数情况下并不理想。

Parallel example

Now, the problem with the above solution is that it's slow - the requests run serially. That's not ideal most of the time.

如果您不需要上一次请求的结果,请继续执行 Promise.all 发出并行请求。

If you don't need the result of the previous request for the next request, just go ahead and do a Promise.all to fire parallel requests.

const users = [1, 2, 3, 4]

const pendingPromises = []
for (const idUser of users) {
  // Here we won't `await` on *each and every* request.
  // We'll just prepare it and push it into an Array
  pendingPromises.push(rp('http://foo.com/users/' + idUser))
}

// Then we `await` on a a `Promise.all` of those requests
// which will fire all the prepared promises *simultaneously*,
// and resolve when all have been completed
const results = await Promise.all(pendingPromises)



错误处理



中的错误处理async / await 由普通的 try..catch 块,为简洁起见我省略了。

Error handling

Error handling in async/await is provided by plain-old try..catch blocks, which I've omitted for brevity.

这篇关于节点异步循环 - 如何使此代码按顺序运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-14 23:45