Я нашел его и подтвердил, что Promise и async / await хорошо работают.
Любые предложения приветствуются и приветствуются, если у вас есть другие лучшие способы.
* address.js
'use strict';
module.exports = function (Address) {
async function getBalanceFromBlockchainInfo(address) {
const blockchaininfo = Address.app.dataSources.blockchaininfo;
try {
const data = await Promise.all([
blockchaininfo.balance(address),
blockchaininfo.balance(address)
])
return { "balance": data[0][address]["final_balance"], "unconfirmed_balance": data[0][address]["final_balance"] };
} catch (err) {
return err;
}
}
Address.getBalance = function (address, callback) {
getBalanceFromBlockchainInfo(address).then(data => callback(null, data)).catch(err => callback(err));
};
Address.remoteMethod("getBalance", {
http: { verb: "get", path: "/:address/balances" },
accepts: [
{ arg: "address", type: "string", required: true }
],
returns: { type: "object", root: true }
});
};
* datasources.json
{
"db": {
"name": "db",
"connector": "memory"
},
"blockchaininfo": {
"name": "blockchaininfo",
"connector": "rest",
"operations": [
{
"template": {
"method": "GET",
"url": "https://blockchain.info/balance",
"query": {
"active": "{address}"
}
},
"functions": {
"balance": [
"address"
]
}
}
]
}
}
https://github.com/y-zono/loopback-connector-rest-example