Recently, when looking at other people's promise source code, some of the code is not very clear, I hope Daniel can help.

var ret = typeof onFulfilled === "function" && onFulfilled(value) || value;

what kind of assignment is it to change this code into ret? I hope I can make it clear. Thank you. Ha

Mar.19,2021

https://www.cnblogs.com/yy-hh.
look at this should help you.


do you want to ask what this code means?
suppose that if typeof onFulfilled = 'function' is regarded as true, then the expression becomes:
var ret = true & & onFulfilled (value) | | value
then there is an "and / OR" operation. In google


function getRet(value) {
    if(typeof onFulfilled === 'function') {
        var fulfilledValue = onFulfilled(value)
        if(fulfilledValue) {
            return fulfilledValue
        } else {
            return value
        }
    } else {
        return value
    }
}
var ret = getRet(value)

js & & the preceding value is true (= = true), the backward execution will continue. | | the front value false (= = false) will continue to execute backward. Understand for yourself


if the data type of onFulfilled is function , execute onFulfilled with the parameter value , assign the result returned by the function to ret , otherwise assign value to ret

.
Menu