Promise

参考文章http://blog.guowenfh.com/2018/06/04/2018/learning-Promise/

promise对象用于表示一个异步操作的最终状态,以及返回的值

链式回调,不用再写callback

request(1)
.then(data=>{
    console.log('promise',data)
    return request(2)
})
.then(data=>{
    console.log('promise',data)
    return request(3)
})
.then(data=>{
    console.log('promise',data)
})
.catch(data=>{
    console.log('error')
})
function request(e){
    return new Promise((resolve,reject)=>{
        setTimeout(()=>{
            switch (e) {
                case 1:
                    resolve('success1');
                    break;
                case 2:
                    resolve('success2');
                    break;
                case 3:
                    reject('success3');
                    break;
                default:
                    break;
            }
        },1000)
    })
}
//promise success1
//promise success2
//error

Promise对象上的方法

Promise.all()会将多个 Promise 实例封装成一个新的 Promise 实例,新的 promise 的状态取决于多个 Promise 实例的状态,只有在全体 Promise 都为 fulfilled 的情况下,新的实例才会变成 fulfilled 状态。;如果参数中 Promise 有一个失败(rejected),此实例回调失败(rejecte),失败原因的是第一个失败 Promise 的结果。

fetch('https://cnodejs.org/api/v1/topics?tab=good&limit=10')
.then(res=>res.json())
.then(res=>{
    const fecthList = res.data.map(t=>{
        return fetch(`https://cnodejs.org/api/v1/topic/${t.id}`)
        .then(res=>res.json())
        // .then(res=>res.data) //return res.data
    })
    Promise.all(fecthList).then(list=>{ //返回一个数组
        console.log(list)
    })
})

Promise.race Promise.race 也会将多个 Promise 实例封装成一个新的Promise实例,只不过新的 Promise 的状态取决于最先改变状态的 Promise 实例的状态。

fetch api模拟请求超时

Promise.race([
    fetch('https://cnodejs.org/api/v1/topics?tab=good&limit=10'),
    new Promise((resolve,reject)=>{
        setTimeout(reject,1000,'error')
    })
]).then(result=>{
    console.log('then',result)
}).catch(error=>{
    console.log('catch',error)
})

关于执行顺序

//在使用 Promise 构造函数构造 一个 Promise 时,回调函数中的内容就会立即执行,而 Promise.then 中的函数是异步执行的。
new Promise((resolve,reject)=>{
    console.log('step 1')
    resolve()
    console.log('step 2')
}).then(()=>{
    console.log('step 3')
})
console.log('step 4')
//step 1
//step 2
//step 4
//step 3

关于异步请求

一般可以使用异步请求来实现等待接口返回后函数再返回参数

const fetch = require('node-fetch')
async function getApi(){
    var a = false
    await fetch('http://127.0.0.1:8088/test')
        .then(function(res){
            return res.json();
        })
        .then(function(res){
            console.log('request res==',res);
            a = res.success;
        })
    return a
}
function getApi(){
    var a = false
    return fetch('http://127.0.0.1:8088/test')
        .then(function(res){
            return res.json();
        })
        .then(function(res){
            console.log('request res==',res);
            a = res.success;
            return a
        })
}
var b = getApi();
b.then((res)=>{
    console.log(res);
})
console.log('b====',b); 
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • Promise 对象 Promise 的含义 Promise 是异步编程的一种解决方案,比传统的解决方案——回调函...
    neromous阅读 12,774评论 1 56
  • title: promise总结 总结在前 前言 下文类似 Promise#then、Promise#resolv...
    JyLie阅读 14,205评论 1 21
  • 目录:Promise 的含义基本用法Promise.prototype.then()Promise.prototy...
    BluesCurry阅读 5,323评论 0 8
  • Promise对象是一种解决异步问题的方法,还有的解决方案是asyns 和 await (es7) 这么是目前的终...
    站在大神的肩膀上看世界阅读 5,010评论 0 6
  • 要理解现在,先要理解过去。 传统时代,企业以产品为导向,营销是商家自卖自夸的形式,消费者存在怀疑。企业的营销注重群...
    会飞的小虾米阅读 2,812评论 0 1