【发布时间】:2018-04-24 02:44:42
【问题描述】:
我正在尝试编写一个简短的应用程序来测试 IOTA 纳米交易。谈到异步编程,我是个菜鸟。请原谅我没有看到明显的内容。
我的问题:如何在适当的时候将回调函数中的值赋给对象。
以下代码说明: 这些是我的 nodejs express 应用程序的片段。我使用控制器、路由器和我的钱包模型。
当使用 iota 库请求服务 (iri) 时,我只能使用异步函数。在以下情况下,我想为我的钱包接收一个 IOTA 地址。这适用于服务,我可以通过将生成的地址写入 console.log 来测试它。
但是,可能是因为回调函数在所有其他函数之后执行,我 只是找不到将其写入我的钱包对象并接收它以在我的钱包中显示它的方法 showPaymentInstructionsAction。
到目前为止我的解释:
- 使用 (callback).bind(this) 我可以将值分配给 目的。
- 该值从未显示在 showPaymentInstructionsAction(当前是一个简单的网页)中,因为该页面是在回调函数执行之前呈现的异步性质。
你能帮我什么忙?
我错过了异步编程中的基本模式吗?
我不应该尝试从回调中接收值吗?
我应该了解 Promise 来解决这个问题吗?
最好的问候, 彼得对象
'use strict'
class Wallet{
constructor(iota_node, seed){
this.seed = seed;
this.receivingAddress = "empty";
this.iota_node = iota_node;
}
generateAddress() {
this.iota_node.api.getNewAddress(this.seed, {'checksum': true}, postResponse)
function postResponse(error,address) {
if (!error) {
// callback won't assigned to my wallet object.
// I can probably use (function).bind(this);
// But this doesn't solve the timing issue
this.receivingAddress = address
// console.log shows, the address is generated correctly
// but how can I get it into my object? and retreive it after it is written?
console.log("address callback: %s", this.receivingAddress)
}else{
console.log(e.message);
}
}
}
getReceivingAddress(){
// I never managed to get this filled by the callback
console.log("in getReceivingAddress: %s", this.receivingAddress)
return this.receivingAddress;
}
}
// The controller
var config = require('../config.js'),
Wallet = require('./model_wallet'),
IOTA = require('iota.lib.js');
function orderRequestAction(req, res, next){
// IOTA Reference Implementation
var iri = new IOTA({
'host': 'http://localhost',
'port': 14265
});
res.locals.wallet = new Wallet(iri, config.wallet.seed);
res.locals.wallet.generateAddress()
}
function showPaymentInstructionsAction(req, res){
res.render('paymentInstructions', {
title:"payment instructions",
receivingAddress: res.locals.wallet.getReceivingAddress()
})
}
// Router
var controller = require('./controller');
module.exports = function(app){
app.post('/orderRequest', controller.orderRequestAction, controller.showPaymentInstructionsAction);
};
【问题讨论】:
标签: javascript node.js asynchronous callback