I am trying to write a short app for testing IOTA nano transactions. When it comes to asynchronous programming, I am a noob. Please forgive me for not seeing the obvious.
My problem: How to assign a value from a callback function to an object at the right moment.
Explanation of the code below: Those are fragments from my nodejs express app. I use a controller, router and my wallet model.
When requesting a service (iri) using the iota library I have only asynchronous functions at my disposal. In the case below, I want to receive an IOTA address for my wallet. This works well with the service, I can test it by writing the generated address to the console.log.
However, probably because the callback function is executed after all other functions, I just can't find a way to write this to my wallet object and receive it for displaying it in my showPaymentInstructionsAction.
My explanation so far:
- With (callback).bind(this) I can probably assign the value to the object.
- The value is never shown in the showPaymentInstructionsAction (currently a simple web page), because the page is rendered before the callback function is executed by it's asynchronous nature.
How can you help me?
Do I miss a basic pattern in async programming?
Should I not try to receive a value from a callback?
Should I learn about Promise to solve this?
Best Regards, Peterobjec
'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);
};