美文网首页
promise async awit

promise async awit

作者: 小小小前端 | 来源:发表于2018-06-13 18:36 被阅读0次

Pramise

function getData( url ) {
    return new Promise( ( resolve, reject ) => {
        $.get( {
            url: url,
            success: function ( data ) {
                resolve( data )
            }
        } )
    } )
}
getData( "http://jsonplaceholder.typicode.com/posts?userId=2" ).then( () => {
    console.log( 111 )
    return getData( "http://jsonplaceholder.typicode.com/posts?userId=1" )
} ).then( ( data ) => {
    console.log( 222 )
    console.log( data )
} )

promise 对象异步操作成功之后,将then方法的函数传入作为resolve回调函数调用,第一个then方法作为回调函数调用之后又返回了一个promise对象,既可以链式调用then方法

相关文章

网友评论

      本文标题:promise async awit

      本文链接:https://www.haomeiwen.com/subject/piszsftx.html