puppeteer701
puppeteer701

Reputation: 1285

Build promises from JSON with bluebird

I have a uniq issue. I have JSON data which represents the steps in generating a promise.

[
  {
     action: 'wait' 
  },
  {
     action: 'screenshot' 
  }
]

Now I am generating this promise with Bluebird.

var pr = require('bluebird/js/main/promise')();
var R = require('bluebird');

var Worker = {
  open: function () {
    return new pr(function (resolve, reject) {
      resolve('opened');
    });
  },
  wait: function (milliseconds) {
    return pr.delay(milliseconds);
  },

  screenshot: function (milliseconds) {
    return new pr(function (resolve, reject) {
      resolve('take screenshot');
    });
  }
}

var defers = [];
Worker.open();
JSON.forEach(function (vl) {
  var defer = R.defer();
  defers.push(defer.promise);
  Worker[vl.action]().then(function () {
    // do something
    defer.resolve();
  });
});
R.all(defers).then(function () { console.log('finished');});

Now this doesn't work, because promises are not chained. I was suggested that I chain them.

 var pr = require('bluebird/js/main/promise')();
var R = require('bluebird');

var Worker = {
  open: function () {
    return new pr(function (resolve, reject) {
      resolve('opened');
    });
  },
  wait: function (milliseconds) {
    return pr.delay(milliseconds);
  },

  screenshot: function (milliseconds) {
    return new pr(function (resolve, reject) {
      resolve('take screenshot');
    });
  }
}

var defers = [];
var chain = Worker.open();
JSON.forEach(function (vl) {
  var defer = R.defer();
  defers.push(defer.promise);
  chain = chain[vl.action]().then(function () {
    // do something
    defer.resolve();
  });
});
R.all(defers).then(function () { console.log('finished')});

But this also doesn't work, any suggestions on how to generate a promise based function from JSON data?

Upvotes: 0

Views: 138

Answers (2)

Madara's Ghost
Madara's Ghost

Reputation: 174977

You want to take the array of actions, and reduce it to a single Promise value.

const actions = [
  {action: 'wait'},
  {action: 'screenshot'}
];

const allDone = actions.reduce((promise, item) => 
  promise.then(Worker[item.action]), Promise.resolve());

allDone.then(() => console.log("All done!"));

This reads as: "Reduce the array by chaining the item's action onto the promise with .then(), starting from an empty Promise."


Per request, with ES5 (Learn ES2015, it's awesome!)

var actions = [
  {action: 'wait'},
  {action: 'screenshot'}
];

var allDone = actions.reduce(function(promise, item) {
  return promise.then(Worker[item.action])
}, Promise.resolve());

allDone.then(function() { 
  console.log("All done!"); 
});

Upvotes: 2

Volune
Volune

Reputation: 4339

I think you got a little confused about chaining promises.

You need to do the next action in the .then(...) of the promise of the previous action.

var chain = Worker.open();
JSON.forEach(function (vl) {
    chain = chain.then(function () {
        //You need to return to promise from Worker[vl.action]() to correctly chain actions
        return Worker[vl.action]();
    });
    //"chain" is now synchronized with the promise of the last chained action
});

Upvotes: -1

Related Questions