我正在编写一个JavaScript函数,它发出HTTP请求并返回结果的承诺(但这个问题同样适用于基于回调的实现).
如果我立即知道为函数提供的参数是无效的,那么函数应该是throw同步的,还是应该返回一个被拒绝的promise(或者,如果你愿意的话,调用一个Error实例的回调)?
异步函数应始终以异步方式运行,特别是对于错误条件,这有多重要?是否确定throw,如果你知道程序是不是一个合适的状态的异步操作继续进行?
例如:
function getUserById(userId, cb) {
if (userId !== parseInt(userId)) {
throw new Error('userId is not valid')
}
// make async call
}
// OR...
function getUserById(userId, cb) {
if (userId !== parseInt(userId)) {
return cb(new Error('userId is not valid'))
}
// make async call
}
Run Code Online (Sandbox Code Playgroud) 当我有以下代码时:
var promise1 = Promise.resolve([1, 2, 3]);
promise1.then((value) => {
console.log(value);
// expected output: Array [1, 2, 3]
});
console.log('end of script');Run Code Online (Sandbox Code Playgroud)
我知道脚本的结尾是早先返回的,因为它promise是异步的.但是在执行的哪个阶段它变得异步?
是Promise.resolve()异步的?或者是 .then异步还是两种功能?引擎盖下是否还有其他一些机制?
(谷歌是一个地狱,因为我只获得了新async await功能的结果)
我已经看到本机ES6 Promise.resolve()可以直接调用 - 作为静态方法.Facebook正在他们的Flux示例中使用它.
但在什么情况下我应该这样做?排队的东西?或者不是使用window.setTimeout()?
(相关但不完全相同:JS Promises:Fulfill vs Resolve)
我一直在尝试围绕Javascript承诺,我正在努力解决和解决的基本概念,而不是履行和实现.我已经阅读了几个介绍,例如Jake Archibald,以及浏览一些 相关的 规范.
在国家和命运(不是官方规范,但引用作为规范作者之一的权威文件),履行是一个国家,而解决是一个"命运"(无论是什么 - 但他们明显不同) :
承诺有三种可能的互斥状态:履行,拒绝和待决.
- 如果能尽快给f打电话,我们就会履行承诺
promise.then(f).
和
如果试图解决或拒绝承诺,则承诺得到解决,即承诺已被"锁定"以遵循另一承诺,或已履行或拒绝
特别是,已解决包括已履行和已拒绝(并已锁定).拒绝的"相反"(或直接相应的功能)是满足,而不是解决 ; 解决 包括 拒绝作为其可能性之一.
然而,规范引用了then()使用完成和解析的方法(或其相应的抽象概念)的第一个参数:
25.4:如果p.then(f …
var p1 = new Promise (function (res, rej){
res(42);
}).then((result) => {return result;});
Run Code Online (Sandbox Code Playgroud)
**如果我有** return result,
这个承诺有没有解决?“已兑现的承诺”是什么意思?
鉴于客户端程序员既定义又使用resolve传递给Promise的执行器的函数,我天真地假设我完全控制了resolve函数的签名,因此我可以定义它接受多个参数。
然而以下代码(jsFiddle)失败了:
<html>
<script>
const promise = new Promise(function executor(resolve, reject) {
setTimeout(function () {
resolve(42, true);
}, 1000);
});
promise.then(function resolveCallback(answer, howCertain) {
console.log('callback called with arguments: ('+answer+', '+howCertain+')');
console.log('the answer is: '+answer+'. I am '+(howCertain?'very':'not very')+' certain about that');
});
</script>
</html>
Run Code Online (Sandbox Code Playgroud)
上面的代码实际上打印在控制台上:
callback called with arguments: (42, undefined)
the answer is: 42. I am not very certain about that
Run Code Online (Sandbox Code Playgroud)
再深入一点,我写了下面的代码(jsFiddle):
<html>
<script>
const promise = new Promise(function …Run Code Online (Sandbox Code Playgroud) \n\n沿链出现的 thenable 对象
\nthen()始终会被解析\n\xe2\x80\x94,onFulfilled处理程序永远不会接收到 thenable 对象,并且任何一个处理程序返回的任何\nthenable 始终会在传递到下一个处理程序之前被解析。这是因为在构造新的 Promise 时,传递resolve的 和reject函数会被executor保存,并且当当前 Promise 结算时,将使用履行值或拒绝原因调用相应的函数。解析逻辑来自构造函数传递的解析器函数> Promise()。
据我了解,承诺并不总是能得到解决,也可能被拒绝。
\n这个文档是什么意思,有错误吗?
\n考虑以下包含 Bluebird 的 Promise.settle 的简化实现的代码:
var a = Promise.reject('a');
var b = Promise.resolve('b');
var c = Promise.resolve('c');
var promises = [a,b,c];
function settled(promises) {
var alwaysFulfilled = promises.map(function (p) {
return p.then(
function onFulfilled(value) {
return { state: 'fulfilled', value: value };
},
function onRejected(reason) {
return { state: 'rejected', reason: reason };
}
);
});
return Promise.all(alwaysFulfilled);
}
//Update status message once all requests finish
settled(promises).then(function (outcomes) {
var count = 0;
outcomes.forEach(function (outcome) {
if (outcome.state == 'fulfilled') …Run Code Online (Sandbox Code Playgroud) javascript ×7
promise ×4
asynchronous ×3
es6-promise ×2
node.js ×2
api-design ×1
bluebird ×1
callback ×1
ecmascript-6 ×1
terminology ×1