Is there any way to call Multichain API in synchronously in node JS ?

+1 vote
Is there any way to call Multichain API in synchronously in node JS ? I am using multichain apis using Multichain-node where every call to multichain is going in Async way. How can i call it in Synchronous way ?
asked Dec 10, 2018 by sufyan ahmad

1 Answer

+3 votes

You can try using either:

  1. Promises: https://developers.google.com/web/fundamentals/primers/promises
  2. Await, Async Model: https://javascript.info/async-await

I think Multichain itself doesn't support it natively because blockchain are generally multithreaded and async

answered Dec 11, 2018 by SDVII
I used async and awiat but did not return me any response. please look at this code
 router.post('/getAddressBalances', authenticate,async function (req, res) {
  var  err, info= await multichain.getAddressBalances({ address: data.address })
    if (err) {
      console.log(err);
      res.status(400).send({ error: err.message });
    }
    else {
      console.log(info);
      res.send(info);
    }
})
I am not sure why it isn't working for you because I only have this much code to work with. However, did you try and call MC's native RPC API instead of using the community SDK?

Oh, can you please share your MC version?
I am following this link
https://github.com/scoin/multichain-node
And how i can call  MC's native RPC API ?
MC version is :1.0.6
 
http://prntscr.com/ltxc00
First of all, you can call the RPC the same way you call any API, jQuery and axios are good options. It would have this kind of structure:

$.ajax({
    url: 'url of your node with port',
    type: 'POST',
    contentType: 'application/json',
    data: JSON.stringify( your call parameters including method name and required data ),
    success: function(response){
       console.log(response)
    },
    error: function(){
        alert('error');
    }
});

Second of all, the SDK you are using is a community made one, so you might run into some issues, not to mention that it doesn't support the newest beta versions of MC
Oh, that's looking nice. Let me try in that way. Thanks @SDVII
...