是否有可能在蓝鸟地图中绑定它?

时间:2018-04-18 14:33:27

标签: node.js bluebird

所以我试图尽我所能写出一个我想做的事情的例子,这不是一个非常实用的例子,但我试图简化它,但我感觉像在试图做出一个例子时,我可能会把这复杂化。



class add {
  constructor(baseValue) {
    this.base = baseValue;
    return new Promise((resolve, reject) => {
      resolve(this);
    });
  }
  addBase(num) {
    return new Promise((resolve, reject) => {
      resolve(this.base + num);
    });
  }
}

const values = [1,2,3,4,5];

Promise.try(() => {
  return new add(5);
}).then((add) => {
  // we want to find if a 5 exists in the results
  const addPromise = Promise.resolve(values).map(add.addBase, {concurrency: 1});
  return Promise.try(() => {
    return addPromise;
  }).then((results) => {
    for(let i = 0; i < results.length; i++) {
      if(results[i] === 10) {
        return i;
      }
    }
    // doesn't exist
    return null;
  });
}).then((result) => {
  if(result === null) {
    console.log('10 does not exist');
  } else {
    console.log('10 is at position ' + result);
  }
})
&#13;
<script src="https://cdn.jsdelivr.net/bluebird/latest/bluebird.min.js"></script>
&#13;
&#13;
&#13;

如果你运行它,你会得到一个错误,你不能得到未定义的基础,这是因为蓝鸟的映射。 const addPromise = Promise.resolve(values).map(add.addBase, {concurrency: 1});在进行这些调用时,这行是否有办法将添加对象绑定到this

2 个答案:

答案 0 :(得分:1)

你几乎回答了自己的问题......

&#13;
&#13;
class add {
  constructor(baseValue) {
    this.base = baseValue;
    return new Promise((resolve, reject) => {
      resolve(this);
    });
  }
  addBase(num) {
    return new Promise((resolve, reject) => {
      resolve(this.base + num);
    });
  }
}

const values = [1,2,3,4,5];

Promise.try(() => {
  return new add(5);
}).then((add) => {
  // we want to find if a 5 exists in the results
  const addPromise = Promise.resolve(values).map(add.addBase.bind(add), {concurrency: 1});
  return Promise.try(() => {
    return addPromise;
  }).then((results) => {
    for(let i = 0; i < results.length; i++) {
      if(results[i] === 10) {
        return i;
      }
    }
    // doesn't exist
    return null;
  });
}).then((result) => {
  if(result === null) {
    console.log('10 does not exist');
  } else {
    console.log('10 is at position ' + result);
  }
})
&#13;
<script src="https://cdn.jsdelivr.net/bluebird/latest/bluebird.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:1)

这实际上比你想象的更简单。您正在将原始函数传递给map(),但您可能应该传递箭头函数。考虑一下这个简单的类和代码,它通过传递map()来尝试使用add()

&#13;
&#13;
class Test{
    constructor(n) {
        this.n = n
    }
    add(k) {
        return this.n + k
    }
}
let t = new Test(10)
let arr = [1, 2, 3]

// error TypeError: undefined is not an object (evaluating 'this.n')
arr.map(t.add)
&#13;
&#13;
&#13;

这会引发错误,因为地图不是从对象调用add(),它只是认为它是一个函数。一个简单的解决方法是像这样调用地图:

&#13;
&#13;
class Test {
  constructor(n) {
    this.n = n
  }
  add(k) {
    return this.n + k
  }
}
let t = new Test(10)

let arr = [1, 2, 3]
let mapped = arr.map((n) => t.add(n))
console.log(mapped)
&#13;
&#13;
&#13;

你也可以使用:

let mapped = arr.map(t.add.bind(t))

但对我而言,很难快速阅读和理解。我不确定代码中所有已立即解决的承诺是什么,但更改调用map()的方式会使该错误消失。 (稍后您引用的i不在范围内,会出现另一个错误。)