当前位置:网站首页>Promise (II)

Promise (II)

2022-04-23 16:59:00 Endless cake

Promise Use

    <script>
        // Create a new Promise 
        const p = new Promise((resolve, reject) => { // Actuator functions 
            // 2. Perform asynchronous operation tasks 
            // 3.1  If it works , call resolve(value)
            // 3.2  If it works , call reject(reason)
            setTimeout(() => {
                const time = Date.now() // If the current time is even, it means success , Otherwise, it means failure 
                if (time % 2 == 0) { // success 
                    resolve(' Success data ,time=' + time)
                } else { // Failure 
                    reject(' Failed data ,time=' + time)
                }
            }, 1000)

        })
        p.then(
            value => { // Receive successful value data  onResolved
                console.log(' Successful callback ', value)
            },
            reason => { // Receive successful reason data  ovRejected
                console.log(' Failed callback ', reason)
            }
        )
    </script>

版权声明
本文为[Endless cake]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230554519722.html