有没有清除.thenJavaScript Promise实例的方法?
我在QUnit上编写了一个JavaScript测试框架.框架通过在a中运行每个框架来同步运行测试Promise.(抱歉这个代码块的长度.我尽可能地评论它,所以感觉不那么乏味.)
/* Promise extension -- used for easily making an async step with a
timeout without the Promise knowing anything about the function
it's waiting on */
$$.extend(Promise, {
asyncTimeout: function (timeToLive, errorMessage) {
var error = new Error(errorMessage || "Operation timed out.");
var res, // resolve()
rej, // reject()
t, // timeout instance
rst, // reset timeout function
p, // the promise instance
at; // the returned asyncTimeout instance
function …Run Code Online (Sandbox Code Playgroud) https://github.com/promises-aplus/cancellation-spec上最早的问题是(在撰写本文时)9个月之前.我真的找不到关于"标准"承诺的取消功能的可靠信息来源.
到目前为止看起来这个功能是在bluebird中实现的,但作为一个库开发人员,我不想让我的包与完整的promise实现混乱.
我想做的只是传递一个承诺,并支持取消规范.
我在哪里可以找到这些信息?
我正在使用JavaScript中的promises并试图宣传setTimeout函数:
function timeout(ms) {
return new Promise(function(resolve, reject) {
setTimeout(function() {
resolve('timeout done');
}, ms);
});
}
var myPromise=timeout(3000);
myPromise.then(function(result) {
console.log(result); // timeout done
})
Run Code Online (Sandbox Code Playgroud)
相当简单,但我想知道如何在诺言结算之前取消我的超时.timeout返回Promise对象因此我松开了对setTimeout返回值的访问权限,并且无法通过取消超时clearTimeout.最好的方法是什么?
顺便说一句,这没有真正的目的,我只是想知道如何处理这个问题.我也把它放在这里http://plnkr.co/edit/NXFjs1dXWVFNEOeCV1BA?p=preview
我有一个名为"Item"的组件,它在挂载时创建并调用promise.
class Item extends React.Component{
constructor(props){
super(props)
this.onClick = this.onClick.bind(this)
this.prom = new Promise((resolve, reject) => {
setTimeout(() => resolve("PROMISE COMPLETED "+this.props.id),6000)
})
}
componentDidMount(){
this.prom.then((success) => {
console.log(success)
})
}
componentWillUnmount(){
console.log("unmounted")
}
onClick(e){
e.preventDefault()
this.props.remove(this.props.id)
}
render(){
return (
<h1>Item {this.props.id} - <a href="#" onClick={this.onClick}>Remove</a></h1>
)
}
}
Run Code Online (Sandbox Code Playgroud)
如您所见,promise在调用后6秒调用解析.
另一个名为"List"的组件负责在屏幕上显示这些项目."List"是"Item"组件的父级.
class List extends React.Component{
constructor(props){
super(props)
this.state = {
items : [1,2,3]
}
this.handleRemove = this.handleRemove.bind(this)
}
handleRemove(id){
this.setState((prevState, props) => ({
items : prevState.items.filter((cId) => cId != …Run Code Online (Sandbox Code Playgroud) var promise1 = new Promise(function(resolve, reject) {
// ajax api call
});
var promise2 = new Promise(function(resolve, reject) {
// ajax api call
});
Run Code Online (Sandbox Code Playgroud)
我希望能够做一些像 -
if(a < b) {
promise1.cancel();
}
Run Code Online (Sandbox Code Playgroud) 题:
是否有一种"简单"的方法来取消AngularJS中的($q- /$http- )承诺或确定承诺得到解决的顺序?
例
我有一个长时间运行的计算,我通过请求结果$http.某些操作或事件要求我在$http解决初始承诺之前重新开始计算(从而发送新请求).因此,我想我不能使用像这样的简单实现
$http.post().then(function(){
//apply data to view
})
Run Code Online (Sandbox Code Playgroud)
因为我无法确保响应按照我发送请求的顺序返回 - 毕竟我想在所有承诺得到妥善解决时显示最新计算的结果.
但是我想避免等待第一个响应,直到我发送这样的新请求:
const timeExpensiveCalculation = function(){
return $http.post().then(function(response){
if (isNewCalculationChained) {return timeExpensiveCalculation();}
else {return response.data;}
})
}
Run Code Online (Sandbox Code Playgroud)
思考:
使用时,$http我可以访问响应上的config-object,使用一些时间戳或其他标识符来手动排序传入的响应.但是我希望我能以某种方式告诉角度取消过时的承诺,因此在解决时不会运行.then()函数.
如果没有$q-promises的手动实现,这不起作用$http.
也许只是拒绝承诺就是要走的路?但在这两种情况下,它可能需要永远,直到最终在生成下一个请求之前解决了一个promise(在此期间导致一个空视图).
是否存在一些我缺少的角度API函数,或者是否存在强大的设计模式或带有promise链接的"技巧"或$ q.all来处理返回"相同"数据的多个promise?
我有useEffect一个回调函数,可以从多个 API 中获取一些数据。为此,我正在使用Promise并且我的代码如下所示:
useEffect(() => {
const fetchAllData = async () => {
const resourceOne = new Promise((resolve) => {
// fetching data from resource one and changing some states
})
const resourceTwo = new Promise((resolve) => {
// fetching data from resource two and changing some states
})
const resourceThree = new Promise((resolve) => {
// fetching data from resource three and changing some states
})
await Promise.all([resourceOne, resourceTwo, resourceThree])
.then(() => {
// changing …Run Code Online (Sandbox Code Playgroud) Promise.race( list_of_promises ) 返回一个承诺,其中包含列表中“最快”承诺的解析/拒绝结果。
我的问题是其他承诺会怎样?(那些输掉比赛的人……)
使用 node.js 在控制台模式下进行测试似乎表明它们继续运行。
这似乎与没有办法“杀死”承诺的事实是一致的。(我的意思是据我所知,程序员没有办法使用)。
它是否正确 ?
如何拒绝延迟的承诺:
const removeDelay = Promise.delay(5000).then(() => {
removeSomething();
});
//Undo event - if it is invoked before 5000 ms, then undo deleting
removeDelay.reject(); // reject is not a method
Run Code Online (Sandbox Code Playgroud) 我有一个名为 delay() 的 Typescript 函数,它在异步/等待模式下调用。
play(){
(async () => {
await this.delay(90000);
this.player.play();
})();
}
delay(ms: number) {
return new Promise(resolve => setTimeout(resolve, ms));
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在完成 90 秒之前“杀死/中断”setTimeout 并在再次调用“播放”函数时再次开始计数?
promise ×9
javascript ×8
cancellation ×5
reactjs ×2
settimeout ×2
angularjs ×1
bluebird ×1
ecmascript-6 ×1
es6-promise ×1
typescript ×1
use-effect ×1