使用循环插入数据不保证顺序结果?

时间:2018-05-07 11:25:11

标签: javascript node.js ecmascript-6

如何使用索引作为标题将数据插入数据库?我认为这是典型的异步问题,但我无法解决。我的顺序不是按顺序插入的。

const { times } = require('lodash')

module.exports = (async () => {

  try {

    const createJob = async (i) => {

      console.log(i) //executed first 1 - 50 first
      const { data } = await axios
        .post('http://localhost:3000/job/create', {
          "title": i,
          "created_at": Date.now()
          })

      if(data) {
        console.log('job created. ', data)
      }
    }

    times(50, (i) => {
        createJob(++i)
    });

  } catch(e) {
    console.log('Error creating ad. ', e)
  }
})()

2 个答案:

答案 0 :(得分:0)

您可以链接您的承诺,以便在上一个createJob完成之后调用每个createJob。您可以创建索引数组并使用array.reduce进行链接。在下面的代码中,我用new Promise(...)替换你的axios调用,仅用于模拟:



var createJob = async (i) => {
    console.log(i) //executed first 1 - 50 first
    const { data } = await new Promise((resolve, reject) => setTimeout(() => resolve({ data: i}), 1000));
    if (data) {
        console.log('job created. ', data);
    }  
    return data;
}

var arr = Array.from(new Array(10), (e, i) => i);
arr.reduce((m, o) => m.then(() => createJob(o)), Promise.resolve());




答案 1 :(得分:0)

您也可以使用Promise.all来解决此问题。基本上,您在数组中保存请求的所有承诺。一旦完成所有请求,您就可以迭代它们。

const axios = require('axios');
module.exports = (async () => {

  try {

    const createJob = async (i, url) => {
      console.log(i, url) //executed first 1 - 50 first
      return axios.get(url);
    }

    const a = ['http://httpbin.org/anything/123', 'http://httpbin.org/anything/456']
    const promiseArray = [];
    for (let j = 0; j < 4; j++) {
      promiseArray.push(createJob(j, a[j % 2]));
    }
    Promise.all(promiseArray).then((result) => {
      console.log('result', typeof (result));
      for (let i = 0; i < result.length; i++) {
        console.log(result[i].data.url);
      }

    });

  } catch (e) {
    console.log('Error creating ad. ', e)
  }
})()

我使用httpbin进行实际调用和随机有效负载,以确保执行顺序始终相同。