0

I am trying to use ecma6 promise to rewrite that simpel $.when

var d1 = $.Deferred();
var d2 = $.Deferred();

$.when( d1, d2 ).done(function ( v1, v2 ) {
    console.log( v1 ); // "Fish"
    console.log( v2 ); // "Pizza"
});

d1.resolve( "Fish" );
d2.resolve( "Pizza" );

Any way to use new Promise ?

regards

The goal is to RESOLVE the promise OUTSIDE the scope of the promise.

yarek
  • 11,278
  • 30
  • 120
  • 219
  • Do you have an actually asynchronous use case? – Bergi May 30 '17 at 14:43
  • Have you tried something? – Bergi May 30 '17 at 14:43
  • 1
    "*The goal is to RESOLVE the promise OUTSIDE the scope of the promise.*" - Why? [You should never need to](https://stackoverflow.com/a/37426491/1048572). What's your [actual problem](https://meta.stackexchange.com/q/66377)? – Bergi May 30 '17 at 17:50

2 Answers2

2

Promises could be written in many ways, depending on your needs, so these 3 examples should be close to your jQuery example:

var d1 = (v)=>new Promise(r=>r(v))
var d2 = (v)=>new Promise(r=>r(v))
var dd1 = d1('Fish')
var dd2 = d2('Pizza')

Promise.all([dd1, dd2]).then(( v )=>{
    console.log( v[0]+', '+v[1] );
});

//or

var d1 = new Promise(r=>r('Fish'))
var d2 = new Promise(r=>r('Pizza'))

Promise.all([d1, d2]).then(( v )=>{
    console.log( v[0]+', '+v[1] );
});

//or

var d1 = Promise.resolve('Fish')
var d2 = Promise.resolve('Pizza')

Promise.all([d1, d2]).then(( v )=>{
    console.log( v[0]+', '+v[1] );
});

OK, so here's how resolve it from outside:

var reolveMe = [];

var d1 = new Promise(r=>reolveMe.push(r))
var d2 = new Promise(r=>reolveMe.push(r))

Promise.all([d1, d2]).then(( v )=>{
    console.log( v[0]+', '+v[1] );
});

reolveMe[0]('a')
reolveMe[1]('b')

//or more OOP

function myPromise() {
  var myResolve;
  this.promise = new Promise(r=>myResolve=r)
  this.resolve = myResolve;
}

var d1 = new myPromise()
var d2 = new myPromise()

Promise.all([d1.promise, d2.promise]).then(( v )=>{
    console.log( v[0]+', '+v[1] );
});

d1.resolve('a')
d2.resolve('b')
Michał Sałaciński
  • 2,256
  • 1
  • 11
  • 10
0

jQuery's when seems to have a few different syntaxes for resolving multiple promises. The closest I can think of would be to use Promise.all.

let p1 = new Promise((resolve) => resolve('Pizza'));
let p2 = new Promise((resolve) => resolve('Fish'));

Promise.all([p1, p2]).then((values) => {
 console.log(values[0]); //Pizza
 console.log(values[1]); //Fish
});

The JS promise API doesn't exactly match what jQuery offers. For example, Promise.defer() is a deprecated method.. There is a deferred example on that MDN page here .

Class implementation based on the MDN snippet:

class Deferred {
  constructor() {
   this.promise = new Promise((resolve, reject) => {
     this.resolve = resolve;
     this.reject = reject
   });
  }
 }
laughingpine
  • 1,039
  • 14
  • 20
  • The goal is to RESOLVE the promise OUTSIDE the scope of the promise. – yarek May 30 '17 at 17:24
  • As I mentioned the deferred method is deprecated. However on that same page there is a compatibility example: https://developer.mozilla.org/en-US/docs/Mozilla/JavaScript_code_modules/Promise.jsm/Deferred#backwards_forwards_compatible. Perhaps that was more what you are looking for? – laughingpine May 30 '17 at 20:14