最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Intermediate value - then is not a function - Stack Overflow

programmeradmin0浏览0评论

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
Add a ment  | 

1 Answer 1

Reset to default 2

Your .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));
发布评论

评论列表(0)

  1. 暂无评论