📜  将任何函数变成承诺 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:24.103000             🧑  作者: Mango

代码示例3
function getName () {

    return new Promise(function (fulfilled, rejected) {

        var name = "John Doe";

        // wait 3000 milliseconds before calling fulfilled() method
        setTimeout ( 
            function() {
                fulfilled( name )
            }, 
            3000
        )

    })

}


// the console writes "John Doe" after 3000 milliseconds
getName().then(function(name){ console.log(name) })