How to convert a jQuery Deferred object to an ES6 Promise

后端 未结 3 899
难免孤独
难免孤独 2020-12-28 13:28

Is this the correct way to convert jQuery Deferred to a Promise?

var p = Promise.resolve($.getJSON(\'api/values\', null));

Are there any ot

3条回答
  •  失恋的感觉
    2020-12-28 13:53

    I would prefer composition:

    const successCb1 = ()=>$.getJSON('api/values'),
    successCb2 = (json)=>alert(json),
    errorCb = (e)=>alert(e);
    Promise
       .resolve()
       .then(successCb1)
       .then(successCb2)
       .catch(errorCb);
    

提交回复
热议问题