Return 使用静态函数的新 Promise

Return New Promise Using Static Function

我正在寻找一种无需创建新承诺即可return实现承诺的快速方法。

我在 A 点(下图)做的事情有静态方法吗

new Promise(function(resolve){
    resolve(data1, data2);
})
.then(function(data1, data2){
    if(something){
        // returns promise
    }
    else{
        // POINT A
        return new Promise(function(fulfill){
            fulfill(data1, data2);
        });    
    }   
})
.then(function(data){
    console.log(data);
});

理想情况下是像 return Promise.fulfill(data1, data2); 这样的东西,这将使我免于编写调用函数的函数。

您正在寻找Promise.resolve(data)

The Promise.resolve(value) method returns a Promise object that is resolved with the given value. If the value is a thenable (i.e. has a then method), the returned promise will "follow" that thenable, adopting its eventual state; otherwise the returned promise will be fulfilled with the value.

顺便说一句,您始终只能传递一个值(您的示例不起作用)。

.then 函数中不需要 return Promise.resolve(x)。这是隐含的。就 return x!