Search code examples
node.jspromisechaining

How to run a promise


I know that't very silly, but how to start a promise chain? I have, for example,

var p = new Promise(function(resolve,reject) {
  setTimeout(function() {
    return resolve("Hi from promise after timeout");
  },1000);
});

How to run it? It should be like that,

when(p)
.then(function(msg) {
  console.log(msg);
})
.catch(function(error) {
  console.error(error);
});

But when is not defined.


Solution

  • You just need to do:

    p.then(function(msg) {
       console.log(msg);
    })
    .catch(function(error) {
      console.error(error);
    });