zl程序教程

您现在的位置是:首页 >  后端

当前栏目

[ES6] Promise

ES6 Promise
2023-09-14 09:00:54 时间

How to use:

export default function getReplies(topicId){
  return new Promise(function( resolve, reject ){
    _getRepliesForTopic(topicId, function(data){
      let replies = data.replies;
      if(replies){
        resolve(replies);
      }else{
        let error = new Error("An error occurred");
        reject(error);
      }
    });
  });
}

 

getReplies(1)
.then(function(replies){
  return replies.filter( reply => !reply.isAbuse );
})
.then(function(filteredReplies){
  console.log( filteredReplies );
})
.catch(function(error){
  console.log(error);
}) ;