Chaining Javascript promises

后端 未结 3 1242
一整个雨季
一整个雨季 2020-12-17 05:23

I\'m trying to understand Promises from the MDN documentation. The first example demonstrates the then and catch methods:

// We def         


        
3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-17 06:02

    This:

    var p2 = p1.then()
    p2.catch()
    

    is the same as this:

    p1.then().catch()
    

    You can also do this:

    p1
     .then(response => response.body)
     .then(body => JSON.parse(body))
     .then(data => console.log(data))
     .catch(e => console.log('something somewhere failed'))
    

提交回复
热议问题