Search
 
SCRIPT & CODE EXAMPLE
 

JAVASCRIPT

turn any function into promise

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) })
Comment

turn any function into promise

function getName () {

    return new Promise(function (fulfilled, rejected) {

        var name = "John Doe";

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

    })

}


async function foo () {

    var name = await getName(); // awaits for a fulfilled result!

    console.log(name); // the console writes "John Doe" after 3000 milliseconds

}


foo() // calling the foo() method to run the code
Comment

turn any function into promise

function getName () {

    return new Promise(function (fulfilled, rejected) {

        var name = "John Doe";

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

    })

}


async function foo () {

    var name = await getName(); // awaits for a fulfilled result!

    console.log(name); // the console writes "John Doe" after 3000 milliseconds

}


foo() // calling the foo() method to run the code
Comment

PREVIOUS NEXT
Code Example
Javascript :: show dynamic data expressjs 
Javascript :: How To: Build a Live Broadcasting Web App 
Javascript :: highlight each occurrence of text 
Javascript :: extracting script elements 
Javascript :: A react component can only return] 
Javascript :: how to make model class for complex json in flutter 
Javascript :: automatic expiry for a document in mongodb 
Javascript :: native module reactnativepushnotification tried to override rnpushnotification 
Javascript :: lib.js 
Javascript :: gtag.js xample 
Javascript :: save date time without timezone to databse javascript 
Javascript :: reply nodejs terminal 
Javascript :: setting a date range using yup on react date picker 
Javascript :: how to load a javascript game from react 
Javascript :: what regular express will match valid internation number 
Javascript :: javascript map shorthand 
Javascript :: jquery toucheswipe 
Javascript :: javascript regex tester online 
Javascript :: jsmodule not installed vuejs webstorm 
Javascript :: Increment counter each time an element is clicked 
Javascript :: jboss-ejb-client.propeties exampe de configuration 
Javascript :: numbers Math 
Javascript :: js append sample 
Javascript :: constantly send a request until a desired response is recieved expressjs 
Javascript :: how to add ajax loading icon in jquery 
Javascript :: Adding Handlers to All Forms 
Javascript :: GetNameOfZone 
Javascript :: remove port number from url node js 
Javascript :: paramters and arguments 
Javascript :: trigger many url calls JavaScript 
ADD CONTENT
Topic
Content
Source link
Name
9+9 =