I have this bit of code:
const promises = new Array(20).fill(null).map(v => {
return c.lockp('foo').then((v => {
const rand = Math.random()*3000;
return new Promise((resolve) => setTimeout(resolve,rand)).then(_ => v);
})
.then(({key, id}) => c.unlockp(key, id)));
});
return Promise.all(promises)
.then(values => {
console.log('all good');
process.exit(0);
});
I am getting this error:
TypeError: (intermediate value)(intermediate value).then is not a function at Array.fill.map.v (/home/oleg/WebstormProjects/oresoftware/live-mutex/.r2g/tests/smoke-test.js:26:6) at Array.map () at /home/oleg/WebstormProjects/oresoftware/live-mutex/.r2g/tests/smoke-test.js:20:43
It should be occurring on 5th line of code in the code snippet above.
I have this bit of code:
const promises = new Array(20).fill(null).map(v => {
return c.lockp('foo').then((v => {
const rand = Math.random()*3000;
return new Promise((resolve) => setTimeout(resolve,rand)).then(_ => v);
})
.then(({key, id}) => c.unlockp(key, id)));
});
return Promise.all(promises)
.then(values => {
console.log('all good');
process.exit(0);
});
I am getting this error:
TypeError: (intermediate value)(intermediate value).then is not a function at Array.fill.map.v (/home/oleg/WebstormProjects/oresoftware/live-mutex/.r2g/tests/smoke-test.js:26:6) at Array.map () at /home/oleg/WebstormProjects/oresoftware/live-mutex/.r2g/tests/smoke-test.js:20:43
It should be occurring on 5th line of code in the code snippet above.
Share Improve this question asked Jul 15, 2018 at 22:34 user7898461user7898461 1-
Make sure that
c.lockp('foo')
returns a promise. – Kosh Commented Jul 15, 2018 at 22:38
1 Answer
Reset to default 2Your .then
is being called on the function with the v
parameter (which is enclosed in the parentheses right before the .then
). Put the .then
outside instead, so that it gets called on the promise chain rather than on the callback:
const promises = new Array(20).fill(null).map(v => {
return c.lockp('foo')
.then(v => {
const rand = Math.random()*3000;
return new Promise((resolve) => setTimeout(resolve,rand)).then(_ => v);
})
.then(({key, id}) => c.unlockp(key, id));