ECMAScript 6 链接承诺
ECMAScript 6 Chaining Promises
我正在尝试链接 promise,但第二个没有调用 resolve 函数。我做错了什么?
function getCustomers(){
let promise = new Promise((resolve, reject) => {
console.log("Getting customers");
// Emulate an async server call here
setTimeout(() => {
var success = true;
if (success) {
resolve( "John Smith"); // got the customer
} else {
reject("Can't get customers");
}
}, 1000);
}
);
return promise;
}
function getOrders(customer) {
let promise = new Promise((resolve, reject) => {
console.log("Getting orders");
// Emulate an async server call here
setTimeout(() => {
var success = true;
if (success) {
resolve("Order 123"); // got the order
} else {
reject("Can't get orders");
}
}, 1000);
}
);
return promise;
}
getCustomers()
.then((cust) => getOrders(cust))
.catch((err) => console.log(err));
console.log("Chained getCustomers and getOrders. Waiting for results");
代码从第二个函数打印“获取订单”,但不打印“订单 123”:
获取客户
链接的 getCustomers 和 getOrders。等待结果
获取订单
Update. 我想在 return 承诺的链式方法之间的控制台上插入打印。我想这样的事情是不可能的:
getCustomers()
.then((cust) => console.log(cust)) //Can't print between chained promises?
.then((cust) => getOrders(cust))
.then((order) => console.log(order))
.catch((err) => console.error(err));
您想链接一个成功处理程序(对于您的 resolve
结果 "Order 123"
),而不是错误处理程序。所以使用 then
而不是 catch
:-)
getCustomers()
.then(getOrders)
.then((orders) => console.log(orders))
.catch((err) => console.error(err));
None 的 promise 被拒绝,因此您的代码中的 console.log(err)
从未被调用。
I wanted to insert the print on the console between chained methods that return promises. I guess something like this is not possible:
getCustomers()
.then((cust) => console.log(cust)) //Can't print between chained promises?
.then((cust) => getOrders(cust))
是的,这是可能的,但你在这里拦截了一条链。所以第二个 then
回调实际上不是用 cust
调用的,而是用第一个 then
回调的结果调用的 - 和 console.log
returns undefined
, getOrders
会遇到一些问题。
你要么做
var customers = getCustomers();
customers.then(console.log);
customers.then(getOrders).then((orders) => …)
或更简单
getCustomers()
.then((cust) => { console.log(cust); return cust; })
.then(getOrders)
.then((orders) => …)
这是使用 ES6 ECMAScript 为 node.js 顺序执行的代码示例。也许有人觉得它有用。
http://es6-features.org/#PromiseUsage
https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
var soapClient = easysoap.createClient(params);
//Sequential execution for node.js using ES6 ECMAScript
console.log('getAllFunctions:');
soapClient.getAllFunctions()
.then((functionArray) => {
return new Promise((resolve, reject) => {
console.log(functionArray);
console.log('getMethodParamsByName:');
resolve();
});
})
.then(() => {
return soapClient.getMethodParamsByName('test1'); //will return promise
})
.then((methodParams) => {
console.log(methodParams.request); //Console log can be outside Promise like here too
console.log(methodParams.response);
console.log('call');
return soapClient.call({ //Return promise
method: 'test1',
params: {
myArg1: 'aa',
myArg2: 'bb'
}
});
})
.then((callResponse) => {
console.log(callResponse); // response data as json
console.log('end');
})
.catch((err) => {
throw new Error(err);
});
我正在尝试链接 promise,但第二个没有调用 resolve 函数。我做错了什么?
function getCustomers(){
let promise = new Promise((resolve, reject) => {
console.log("Getting customers");
// Emulate an async server call here
setTimeout(() => {
var success = true;
if (success) {
resolve( "John Smith"); // got the customer
} else {
reject("Can't get customers");
}
}, 1000);
}
);
return promise;
}
function getOrders(customer) {
let promise = new Promise((resolve, reject) => {
console.log("Getting orders");
// Emulate an async server call here
setTimeout(() => {
var success = true;
if (success) {
resolve("Order 123"); // got the order
} else {
reject("Can't get orders");
}
}, 1000);
}
);
return promise;
}
getCustomers()
.then((cust) => getOrders(cust))
.catch((err) => console.log(err));
console.log("Chained getCustomers and getOrders. Waiting for results");
代码从第二个函数打印“获取订单”,但不打印“订单 123”:
获取客户 链接的 getCustomers 和 getOrders。等待结果 获取订单
Update. 我想在 return 承诺的链式方法之间的控制台上插入打印。我想这样的事情是不可能的:
getCustomers()
.then((cust) => console.log(cust)) //Can't print between chained promises?
.then((cust) => getOrders(cust))
.then((order) => console.log(order))
.catch((err) => console.error(err));
您想链接一个成功处理程序(对于您的 resolve
结果 "Order 123"
),而不是错误处理程序。所以使用 then
而不是 catch
:-)
getCustomers()
.then(getOrders)
.then((orders) => console.log(orders))
.catch((err) => console.error(err));
None 的 promise 被拒绝,因此您的代码中的 console.log(err)
从未被调用。
I wanted to insert the print on the console between chained methods that return promises. I guess something like this is not possible:
getCustomers() .then((cust) => console.log(cust)) //Can't print between chained promises? .then((cust) => getOrders(cust))
是的,这是可能的,但你在这里拦截了一条链。所以第二个 then
回调实际上不是用 cust
调用的,而是用第一个 then
回调的结果调用的 - 和 console.log
returns undefined
, getOrders
会遇到一些问题。
你要么做
var customers = getCustomers();
customers.then(console.log);
customers.then(getOrders).then((orders) => …)
或更简单
getCustomers()
.then((cust) => { console.log(cust); return cust; })
.then(getOrders)
.then((orders) => …)
这是使用 ES6 ECMAScript 为 node.js 顺序执行的代码示例。也许有人觉得它有用。 http://es6-features.org/#PromiseUsage https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Promise
var soapClient = easysoap.createClient(params);
//Sequential execution for node.js using ES6 ECMAScript
console.log('getAllFunctions:');
soapClient.getAllFunctions()
.then((functionArray) => {
return new Promise((resolve, reject) => {
console.log(functionArray);
console.log('getMethodParamsByName:');
resolve();
});
})
.then(() => {
return soapClient.getMethodParamsByName('test1'); //will return promise
})
.then((methodParams) => {
console.log(methodParams.request); //Console log can be outside Promise like here too
console.log(methodParams.response);
console.log('call');
return soapClient.call({ //Return promise
method: 'test1',
params: {
myArg1: 'aa',
myArg2: 'bb'
}
});
})
.then((callResponse) => {
console.log(callResponse); // response data as json
console.log('end');
})
.catch((err) => {
throw new Error(err);
});