简体   繁体   English

spring JavaScript Promises回调函数参数

[英]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 ". 我正在阅读本文档“ 了解JavaScript承诺 ”。 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". 我感到困惑的是onSuccess和onError函数中的参数,分别称为“ greeting”和“ 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?) (也许在sayHello()函数中?)

Thank in advance. 预先感谢。

greeting is the value returned by the promised returned by sayHello . greetingsayHello返回的sayHello返回的值。 error is the error object for the error that may be caused during the execution of the promise. error是执行承诺期间可能引起的错误的错误对象。

A possible implementation of sayHello would be: sayHello的可能实现为:

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. 为了澄清起见,它们没有被称为onSuccessonError ,但是您确实可以这样认为。 greeting is a string . greeting是一个string

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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