M
M
Maxim Ivanov2017-06-08 19:40:37
Node.js
Maxim Ivanov, 2017-06-08 19:40:37

How to rewrite the request-promise callback chain that you want to run away from?

I have a code, I wrote it on my knee, there is a chain of requests, the result of which needs to be passed on to be called. Moreover, at some point you need to wait for the completion of all calls in order to call another asynchronous function.
Here is the code:
https://github.com/splincode/bullshit-code/blob/ma...
That code even constantly throws errors in the console:

*** Error in `node': malloc(): memory corruption (fast): 0x0000000003cfe050 ***
======= Backtrace: =========
/lib/x86_64-linux-gnu/libc.so.6(+0x777e5)[0x7f3c88fd27e5]
/lib/x86_64-linux-gnu/libc.so.6(+0x81d61)[0x7f3c88fdcd61]
/lib/x86_64-linux-gnu/libc.so.6(__libc_malloc+0x54)[0x7f3c88fde5d4]
node(_ZN2v88internal8Malloced3NewEm+0xd)[0xa0956d]
node(_ZNK2v88internal13FeedbackNexus12FindHandlersEPNS0_4ListINS0_6HandleINS0_6ObjectEEENS0_25FreeStoreAllocationPolicyEEEi+0x329)[0x1213139]
node(_ZN2v88internal2IC19UpdatePolymorphicICENS0_6HandleINS0_4NameEEENS2_INS0_6ObjectEEE+0x2f1)[0xf07a91]
node(_ZN2v88internal2IC10PatchCacheENS0_6HandleINS0_4NameEEENS2_INS0_6ObjectEEE+0xd6)[0xf08b86]
node(_ZN2v88internal6LoadIC12UpdateCachesEPNS0_14LookupIteratorE+0xe0)[0xf08e60]
node(_ZN2v88internal6LoadIC4LoadENS0_6HandleINS0_6ObjectEEENS2_INS0_4NameEEE+0x2af)[0xf092cf]
node(_ZN2v88internal19Runtime_LoadIC_MissEiPPNS0_6ObjectEPNS0_7IsolateE+0x2df)[0xf0de2f]
[0x22fe3e0043a7]
======= Memory map: ========
00400000-0204a000 r-xp 00000000 08:02 42730233                           /usr/bin/nodejs
0224a000-0224b000 r--p 01c4a000 08:02 42730233                           /usr/bin/nodejs
0224b000-02263000 rw-p 01c4b000 08:02 42730233                           /usr/bin/nodejs
7f3c74021000-7f3c78000000 ---p 00000000 00:00 0 
7f3c78000000-7f3c780bd000 rw-p 00000000 00:00 0 
7f3c780bd000-7f3c7c000000 ---p 00000000 00:00 0 
7f3c7c000000-7f3c7c1af000 rw-p 00000000 00:00 0 
7f3c7c1af000-7f3c80000000 ---p 00000000 00:00 0 
7f3c80000000-7f3c80075000 rw-p 00000000 00:00 0 
7f3c80075000-7f3c84000000 ---p 00000000 00:00 0 
7f3c86d54000-7f3c86d56000 r-xp 00000000 08:02 8522866                    /home/splincode/Develop/test-loader/node_modules/deasync/bin/linux-x64-node-7/deasync.node
7f3c86d56000-7f3c86f55000 ---p 00002000 08:02 8522866                    /home/splincode/Develop/test-loader/node_modules/deasync/bin/linux-x64-node-7/deasync.node
7f3c86f55000-7f3c86f56000 r--p 00001000 08:02 8522866                    /home/splincode/Develop/test-loader/node_modules/deasync/bin/linux-x64-node-7/deasync.node
7f3c86f56000-7f3c86f57000 rw-p 00002000 08:02 8522866                    /home/splincode/Develop/test-loader/node_modules/deasync/bin/linux-x64-node-7/deasync.node
7f3c86f57000-7f3c86f58000 ---p 00000000 00:00 0 
7f3c86f58000-7f3c87758000 rw-p 00000000 00:00 0 
7f3c87758000-7f3c87759000 ---p 00000000 00:00 0 
7f3c87759000-7f3c87f59000 rw-p 00000000 00:00 0 
7f3c87f59000-7f3c87f5a000 ---p 00000000 00:00 0 
7f3c87f5a000-7f3c8875a000 rw-p 00000000 00:00 0 
7f3c8875a000-7f3c8875b000 ---p 00000000 00:00 0 
7f3c8875b000-7f3c88f5b000 rw-p 00000000 00:00 0 
7f3c88f5b000-7f3c8911a000 r-xp 00000000 08:02 16777572                   /lib/x86_64-linux-gnu/libc-2.23.so
7f3c8911a000-7f3c8931a000 ---p 001bf000 08:02 16777572                   /lib/x86_64-linux-gnu/libc-2.23.so
7f3c8931a000-7f3c8931e000 r--p 001bf000 08:02 16777572                   /lib/x86_64-linux-gnu/libc-2.23.so
7f3c8931e000-7f3c89320000 rw-p 001c3000 08:02 16777572                   /lib/x86_64-linux-gnu/libc-2.23.so
7f3c89320000-7f3c89324000 rw-p 00000000 00:00 0 
7f3c89324000-7f3c8933c000 r-xp 00000000 08:02 16777583                   /lib/x86_64-linux-gnu/libpthread-2.23.so
7f3c8933c000-7f3c8953b000 ---p 00018000 08:02 16777583                   /lib/x86_64-linux-gnu/libpthread-2.23.so
7f3c8953b000-7f3c8953c000 r--p 00017000 08:02 16777583                   /lib/x86_64-linux-gnu/libpthread-2.23.so
7f3c8953c000-7f3c8953d000 rw-p 00018000 08:02 16777583                   /lib/x86_64-linux-gnu/libpthread-2.23.so
7f3c8953d000-7f3c89541000 rw-p 00000000 00:00 0 
7f3c89541000-7f3c89557000 r-xp 00000000 08:02 16781643                   /lib/x86_64-linux-gnu/libgcc_s.so.1
7f3c89557000-7f3c89756000 ---p 00016000 08:02 16781643                   /lib/x86_64-linux-gnu/libgcc_s.so.1
7f3c89756000-7f3c89757000 rw-p 00015000 08:02 16781643                   /lib/x86_64-linux-gnu/libgcc_s.so.1
7f3c89757000-7f3c8985f000 r-xp 00000000 08:02 16777570                   /lib/x86_64-linux-gnu/libm-2.23.so
7f3c8985f000-7f3c89a5e000 ---p 00108000 08:02 16777570                   /lib/x86_64-linux-gnu/libm-2.23.so
7f3c89a5e000-7f3c89a5f000 r--p 00107000 08:02 16777570                   /lib/x86_64-linux-gnu/libm-2.23.so
7f3c89a5f000-7f3c89a60000 rw-p 00108000 08:02 16777570                   /lib/x86_64-linux-gnu/libm-2.23.so
7f3c89a60000-7f3c89bd2000 r-xp 00000000 08:02 42731453                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f3c89bd2000-7f3c89dd2000 ---p 00172000 08:02 42731453                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f3c89dd2000-7f3c89ddc000 r--p 00172000 08:02 42731453                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f3c89ddc000-7f3c89dde000 rw-p 0017c000 08:02 42731453                   /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.21
7f3c89dde000-7f3c89de2000 rw-p 00000000 00:00 0 
7f3c89de2000-7f3c89de9000 r-xp 00000000 08:02 16777579                   /lib/x86_64-linux-gnu/librt-2.23.so
7f3c89de9000-7f3c89fe8000 ---p 00007000 08:02 16777579                   /lib/x86_64-linux-gnu/librt-2.23.so
7f3c89fe8000-7f3c89fe9000 r--p 00006000 08:02 16777579                   /lib/x86_64-linux-gnu/librt-2.23.so
7f3c89fe9000-7f3c89fea000 rw-p 00007000 08:02 16777579                   /lib/x86_64-linux-gnu/librt-2.23.so
7f3c89fea000-7f3c89fed000 r-xp 00000000 08:02 16777568                   /lib/x86_64-linux-gnu/libdl-2.23.so
7f3c89fed000-7f3c8a1ec000 ---p 00003000 08:02 16777568                   /lib/x86_64-linux-gnu/libdl-2.23.so
7f3c8a1ec000-7f3c8a1ed000 r--p 00002000 08:02 16777568                   /lib/x86_64-linux-gnu/libdl-2.23.so
7f3c8a1ed000-7f3c8a1ee000 rw-p 00003000 08:02 16777568                   /lib/x86_64-linux-gnu/libdl-2.23.so
7f3c8a1ee000-7f3c8a214000 r-xp 00000000 08:02 16777573                   /lib/x86_64-linux-gnu/ld-2.23.so
7f3c8a3ea000-7f3c8a3f1000 rw-p 00000000 00:00 0 
7f3c8a40c000-7f3c8a40d000 rw-p 00000000 00:00 0 
7f3c8a40d000-7f3c8a40e000 ---p 00000000 00:00 0 
7f3c8a40e000-7f3c8a413000 rw-p 00000000 00:00 0 
7f3c8a413000-7f3c8a414000 r--p 00025000 08:02 16777573                   /lib/x86_64-linux-gnu/ld-2.23.so
7f3c8a414000-7f3c8a415000 rw-p 00026000 08:02 16777573                   /lib/x86_64-linux-gnu/ld-2.23.so
7f3c8a415000-7f3c8a416000 rw-p 00000000 00:00 0 
7ffee5b7c000-7ffee5b9e000 rw-p 00000000 00:00 0                          [stack]
7ffee5bc0000-7ffee5bc2000 r--p 00000000 00:00 0                          [vvar]
7ffee5bc2000-7ffee5bc4000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]
Aborted (core dumped)

real	0m5.277s
user	0m0.312s
sys	0m0.016s

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Anton, 2017-06-08
@splincodewd

2ality.com/2017/05/util-promisify.html
Promise.all

A
Arman, 2017-06-08
@Arik

google for JS Callback Hell

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question