简体   繁体   中英

spring JavaScript Promises callback function parameter

I guess this is a really basic question for promise, but just need someone to clarify to me. I am reading this documentation " Understanding JavaScript Promises ". In the example the code is as following

var greetingPromise = sayHello();
greetingPromise.then(function (greeting) {
    console.log(greeting);    // 'hello world’
}, function (error) {
    console.error('uh oh: ', error);   // 'uh oh: something bad happened’
});

What I am confused about are the parameters in the onSuccess and onError functions, named "greeting" and "error". What are they, or in the other world where are they defined? How shall I know what I am expecting to get from there? (maybe in the sayHello() function?)

Thank in advance.

greeting is the value returned by the promised returned by sayHello . error is the error object for the error that may be caused during the execution of the promise.

A possible implementation of sayHello would be:

function sayHello(){
    return new Promise((res, rej) => res("hello world"));
}

As a clarification, they are not called onSuccess and onError , but you can indeed think of them as such. greeting is a string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM