react native tips

Demo


        console.log('aaaaa0');
        setTimeout(() => {
            console.log('aaaaa1');
        }, 0);

        console.log('aaaaa3');


        new Promise((resolve, reject) => {
            console.log('aaaaa4')
            resolve('aaaaa4');

            console.log('aaaaa5');
            resolve('aaaaa5');

            console.log('aaaaa6');
            reject('aaaaa6')

            console.log('aaaaa7');
            reject('aaaaa7')
            console.log('aaaaa7.0');
        }).then((result) => {
            console.log('aaaaa8');
            console.log(result);
            console.log('aaaaa9');
        }).catch(e => {
            console.log('aaaaa10');
            console.log(e);
            console.log('aaaaa11');
        });

        console.log('aaaaa12');
        this.setState({
            countryId: 2
        }, () => {
            console.log('aaaaa2', this.state.countryId);
        });
        this.setState({
            countryId: 3
        }, () => {
            console.log('aaaaa13', this.state.countryId);
        });
        console.log('aaaaa14');

The output is above

aaaaa0
aaaaa3
aaaaa4
aaaaa5
aaaaa6
aaaaa7
aaaaa7.0
aaaaa12
aaaaa14
aaaaa2 3
aaaaa13 3
aaaaa8
aaaaa4
aaaaa9
aaaaa1

Interested above can scramble, and then observe the execution sequence

The above results appear, you can temporarily be summarized as the following several results:

setTimeout is a macro task

promise is a micro-task, when a function has multiple resole and reject, the first will really take effect; resole / reject does not mean return, they will be behind the code execution; time will have promise

setState micro tasks, the last task will be performed in the micro, there appeared the first time promise to perform than then; more setState will merge, with a field, the value of the latter will overwrite the previous;

After the task is finished micro, the macro will perform the task;

Guess you like

Origin www.cnblogs.com/shidaying/p/11778712.html