NodeJS - Promise.each错误我无法调试

时间:2018-02-15 18:29:48

标签: node.js promise

使用Bluebird,我一直试图通过Promise.each顺序对数组进行操作,但我不能。实际上,它说如下: ERROR_DESCRIPTION

以下是代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://unpkg.com/imagesloaded@4.1.4/imagesloaded.pkgd.min.js"></script>
<script src="https://unpkg.com/isotope-layout@3.0.5/dist/isotope.pkgd.min.js"></script>

<div class="grid">
  <div class="grid-sizer"></div>
  <div class="grid-item grid-item--width2">
    <img src="http://via.placeholder.com/30x387" alt="">
  </div>
  <div class="grid-item grid-item--width3 ">
    <img src="http://via.placeholder.com/30x387" alt="">
  </div>
  <div class="grid-item ">
    <img src="http://via.placeholder.com/30x387" alt="">
  </div>
  <div class="grid-item ">
    <img src="http://via.placeholder.com/30x387" alt="">
  </div>
  <div class="grid-item grid-item--width3 ">
    <img src="http://via.placeholder.com/30x387" alt="">
  </div>
  <div class="grid-item ">
    <img src="http://via.placeholder.com/30x387" alt="">
  </div>
  <div class="grid-item grid-item--width2 ">
    <img src="http://via.placeholder.com/30x387" alt="">
  </div>
</div>

由于

1 个答案:

答案 0 :(得分:0)

这是您需要使用Promise.each的方法,并且您应该在res.send之外添加promise.each,因为它执行多次,您将最终多次发送res。

'use strict';

const express = require('express');
const router = express.Router();
const Promise = require('bluebird');

router.get('/', function (req, res, next) {

  let test = [1, 2, 3];
  Promise.each(test,(values) => {
    console.log(values);
  });
  res.setHeader('Content-Type', 'application/json');
  res.send(JSON.stringify("okok"));

});

module.exports = router;