您可以在创建后向承诺添加 .then 吗?
Can you add a .then to a promise after it's created?
承诺让我感到困惑。
我正在尝试制作模拟数据服务来模仿 axios。
我的模拟 put
调用将 targetUrl 传递给 _fetch
,然后它会查看它是否有效 url 和 returns 一个带有延迟的新 Promise .resolve
const _returnResponse = (mockData, time = 0) => new Promise((resolve) => {
setTimeout(() => {
resolve(mockData);
}, time);
});
或延迟 .reject 的新 Promise
const _returnError = (time = simulatedDelay) => {
const returnValue = new Promise(((resolve, reject) => {
setTimeout(() => {
reject(new Error('error'));
}, time);
}));
return returnValue;
};
但是当我做我的模拟时 put
调用这个 returns 一个模拟数据,调用方法将其解释为成功并且控制台记录在其 .then
put(target, putBody) {
const returnValue = _fetch(target, simulatedDelay)
returnValue.then(response => _console('PUT', target, response, putBody));
return returnValue;
},
但是使用无效的目标控制台会记录一个未捕获的错误
或者这可以正确处理错误,但控制台会记录未定义的响应
put(target, putBody) {
const returnValue = _fetch(target, simulatedDelay).then(response => _console('PUT', target, response, putBody));
return returnValue;
},
调用方法如下:
saveStuff({ commit, state }, newStuff) {
//other code
return this.$mockAxios.put(url, putBody)
.then((response) => {
return response;
});
},
我觉得我完全遗漏了什么,我研究了好几个小时,但还是没明白。
作为对问题的直接回答:是的,您可以在创建后将 .then() 添加到承诺中。
示例:
const hi = new Promise((resolve, reject) => {
setTimeout(() => resolve('hello'), 2000);
});
hi.then(result => console.log(result));
至于让您感到困惑的承诺,我建议(除了更多阅读之外)在您的 IDE 中使用 setTimeout 进行大量尝试。我知道您已经在您的模拟中使用了 setTimeout,但将其进一步剥离并仅 运行 其自身文件中的代码,以便您控制整个环境。有很多 console.log('blah') 来查看顺序等。还要确保您熟悉回调,因为 promises 只是回调的语法糖。尝试同时阅读 JS 事件循环 - 如果您知道 callback/promise 如何以及何时执行,它可能会提供一些上下文。
请注意,您甚至可以在回调已解决或拒绝后添加 .then()。因此术语 "promise" - 它实际上是一个承诺,你的 .then() 函数将 运行。 https://en.wikipedia.org/wiki/Promise_theory
const hi = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('one second');
resolve();
}, 1000);
});
setTimeout(() => {
hi.then(() => console.log('two seconds. this executes approximately a full second after the first promise has resolved'));
}, 2000);
承诺让我感到困惑。
我正在尝试制作模拟数据服务来模仿 axios。
我的模拟 put
调用将 targetUrl 传递给 _fetch
,然后它会查看它是否有效 url 和 returns 一个带有延迟的新 Promise .resolve
const _returnResponse = (mockData, time = 0) => new Promise((resolve) => {
setTimeout(() => {
resolve(mockData);
}, time);
});
或延迟 .reject 的新 Promise
const _returnError = (time = simulatedDelay) => {
const returnValue = new Promise(((resolve, reject) => {
setTimeout(() => {
reject(new Error('error'));
}, time);
}));
return returnValue;
};
但是当我做我的模拟时 put
调用这个 returns 一个模拟数据,调用方法将其解释为成功并且控制台记录在其 .then
put(target, putBody) {
const returnValue = _fetch(target, simulatedDelay)
returnValue.then(response => _console('PUT', target, response, putBody));
return returnValue;
},
但是使用无效的目标控制台会记录一个未捕获的错误
或者这可以正确处理错误,但控制台会记录未定义的响应
put(target, putBody) {
const returnValue = _fetch(target, simulatedDelay).then(response => _console('PUT', target, response, putBody));
return returnValue;
},
调用方法如下:
saveStuff({ commit, state }, newStuff) {
//other code
return this.$mockAxios.put(url, putBody)
.then((response) => {
return response;
});
},
我觉得我完全遗漏了什么,我研究了好几个小时,但还是没明白。
作为对问题的直接回答:是的,您可以在创建后将 .then() 添加到承诺中。
示例:
const hi = new Promise((resolve, reject) => {
setTimeout(() => resolve('hello'), 2000);
});
hi.then(result => console.log(result));
至于让您感到困惑的承诺,我建议(除了更多阅读之外)在您的 IDE 中使用 setTimeout 进行大量尝试。我知道您已经在您的模拟中使用了 setTimeout,但将其进一步剥离并仅 运行 其自身文件中的代码,以便您控制整个环境。有很多 console.log('blah') 来查看顺序等。还要确保您熟悉回调,因为 promises 只是回调的语法糖。尝试同时阅读 JS 事件循环 - 如果您知道 callback/promise 如何以及何时执行,它可能会提供一些上下文。
请注意,您甚至可以在回调已解决或拒绝后添加 .then()。因此术语 "promise" - 它实际上是一个承诺,你的 .then() 函数将 运行。 https://en.wikipedia.org/wiki/Promise_theory
const hi = new Promise((resolve, reject) => {
setTimeout(() => {
console.log('one second');
resolve();
}, 1000);
});
setTimeout(() => {
hi.then(() => console.log('two seconds. this executes approximately a full second after the first promise has resolved'));
}, 2000);