Usage of then() in js

The then() method is executed asynchronously.

It means: that is, when the method before .then() is executed, the program inside then() is executed, which avoids the problem of data not being obtained.

Syntax: promise.then(onCompleted, onRejected);

parameter

Required for promise. Promise object.

onCompleted Required. A fulfillment handler function to run when the promise completes successfully.

onRejected is optional. Error handler function to run when the promise is rejected

Example

 navigator.mediaDevices.getUserMedia(constrains).then( function (stream) { video.srcObject=stream; }
 It can be understood that navigator.mediaDevices.getUserMedia(constrains) returns a parameter stream, and then executes the function function

The post usage of then() in js first appeared on Lenix Blog .

This article is reprinted from https://blog.p2hp.com/archives/9717
This site is for inclusion only, and the copyright belongs to the original author.