syntax for Jquery.deferred , making synchronous function return promise

强颜欢笑 提交于 2019-12-02 01:38:45

To do something synchronously, but still use a promise, do:

function slowPromise() {

    var def = $.Deferred();

    // do something slow and synchronous
    ...

    // resolve the deferred with the result of the slow process
    def.resolve(res);

    // and return the deferred
    return def.promise();
}

The effect is that you still get a promise, but that promise is already resolved, so any .then() which is subsequently registered on it proceeds immediately.

The advantage of this pattern is that if you subsequently replace the synchronous code with something asynchronous the function still has the same external interface.

If you want to execute a function after the timeout has expired, you need to call resolve() on the Deferred object from within the timeout expiration function:

function sayIt(ms) {
  var d = $.Deferred();
  setTimeout(function() {
      console.log('what I say');
      d.resolve()
    }, ms);
  return d;
}

In this way you constrain the resolution of the Deferred object to the expiration of the timeout.

To achieve what I believe is your intent:

function doIt() {
  return sayIt(2000).promise()
}

The .promise() call is optional. It only restricts the available interface for callers: by returning a Promise instead of the original Deferred object, the caller can only react to events, but not triggering them. Reference: http://api.jquery.com/deferred.promise/.

Eventually, your original call:

doIt().then( function() { console.log('ah'); });

Will output:

// 2 seconds delay
what I say
ah 
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!