javascript - 将属性附加到 Promise

标签 javascript es6-promise

关于 SO 的另一个问题激发了我对创建 setTimeout 的兴趣,它返回一个 Promise 和一个 timerId,但不会破坏向后兼容性。 This是我指的问题。

它只是询问 underscore.js_.delay 方法中的内部 return 语句是无关的还是有目的的。这是代码块。

这是 _.delay 的代码:

// Delays a function for the given number of milliseconds, and then calls
// it with the arguments supplied.
_.delay = function(func, wait) {
    var args = slice.call(arguments, 2);
    return setTimeout(function() { // this is to return the timerId
        return func.apply(null, args); // this guy right is in question
    }, wait);
};

考虑到 setTimeout 当前不返回 Promise,我提出了它可能存在的想法以备将来验证,以防 setTimeout 有一天返回一个 Promise。

要执行此操作,setTimeout 需要返回一个 timerId,以便可以将其取消。因此,要返回 Promise,您需要将 timerId 附加到 Promise,以便返回 Promise 并且可以访问 timerId。

然后您可以修改 clearTimeout 以在给定 timerId 时完全执行现在的操作,但对于 Promise,它使用 Promise.timerId 来清除超时并取消 Promise。 当然也需要实现 Promise 取消...

无论如何...我开始做一些有趣的事情,并遇到了一些我无法解释的事情。如果运行下面的代码片段,您会看到 promise 在返回之前具有 .timerId 属性,但在返回之后该属性丢失了。谁能解释一下?

function pseudoSetTimeout( func, wait ) {

    let timerId,
        promise = new Promise( ( resolve, reject ) => {
            timerId = setTimeout( () => {
                let returnVal = func();
                resolve( returnVal );
            }, wait );
        });

    promise.timerId = timerId;

    console.log( "promise before return: ", promise );

    return promise;
}

function callback() {
    return "Callback fired";
}

let timeout = pseudoSetTimeout( callback, 1000 )
    .then( ( val ) => {
      console.log( val );
    });

console.log( "returned promise: ", timeout );

最佳答案

然后在:

pseudoSetTimeout( callback, 1000 )
    .then( ( val ) => {
      console.log( val );
    });

返回一个新的 promise ,这不是 pseudoSetTimeout 返回的 promise 。这是一个以 undefined 作为 promise 值的 promise ,因为 then 回调不会返回任何内容。

您可以通过在分配期间不应用 then 来使其工作:

function pseudoSetTimeout( func, wait ) {

    let timerId,
        promise = new Promise( ( resolve, reject ) => {
            timerId = setTimeout( () => {
                let returnVal = func();
                resolve( returnVal );
            }, wait );
        });

    promise.timerId = timerId;

    console.log( "promise before return: ", promise );

    return promise;
}

function callback() {
    return "Callback fired";
}

let timeout = pseudoSetTimeout( callback, 1000 );
timeout.then( ( val ) => {
  console.log( val );
});

console.log( "returned promise: ", timeout );

因为 then 在 promise 的使用中至关重要,并且经常用于链接它们,似乎将自定义属性附加到 promise 的想法失去了用处。

关于javascript - 将属性附加到 Promise,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45268945/

相关文章:

javascript - 当 return new Promise((resolve, reject) => {}) 忘记调用 resolve 或 reject 时会发生什么?

javascript - 如何确保Jest bootstrap文件首先运行?

javascript - 使用 contentful CMS 中的 javascript/react 按顺序解决 Promise

Javascript 不删除 div 中的所有元素

javascript - CSS3D矩阵生成

javascript - jquery 比较两个数组

javascript - Typescript 中的可索引类型和数组有什么区别

javascript - Vuetify 中大尺寸 v-checkbox 元素的问题

javascript - 为什么我可以等待此代码但不能使用 .then?

javascript - 那么区 block 不会在链式 promise 中被调用