相关疑难解决方法(0)

如何在回调中访问正确的`this`?

我有一个构造函数,它注册一个事件处理程序:

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', function () {
        alert(this.data);
    });
}

// Mock transport object
var transport = {
    on: function(event, callback) {
        setTimeout(callback, 1000);
    }
};

// called as
var obj = new MyConstructor('foo', transport);
Run Code Online (Sandbox Code Playgroud)

但是,我无法data在回调中访问已创建对象的属性.它看起来this并不是指创建的对象,而是指另一个对象.

我还尝试使用对象方法而不是匿名函数:

function MyConstructor(data, transport) {
    this.data = data;
    transport.on('data', this.alert);
}

MyConstructor.prototype.alert = function() {
    alert(this.name);
};
Run Code Online (Sandbox Code Playgroud)

但它表现出同样的问题.

如何访问正确的对象?

javascript callback this

1309
推荐指数
12
解决办法
36万
查看次数

什么是明确的承诺构建反模式,我该如何避免它?

我编写的代码看起来像:

function getStuffDone(param) {           | function getStuffDone(param) {
    var d = Q.defer(); /* or $q.defer */ |     return new Promise(function(resolve, reject) {
    // or = new $.Deferred() etc.        |     // using a promise constructor
    myPromiseFn(param+1)                 |         myPromiseFn(param+1)
    .then(function(val) { /* or .done */ |         .then(function(val) {
        d.resolve(val);                  |             resolve(val);
    }).catch(function(err) { /* .fail */ |         }).catch(function(err) {
        d.reject(err);                   |             reject(err);
    });                                  |         });
    return d.promise; /* or promise() */ |     });
}                                        | }
Run Code Online (Sandbox Code Playgroud)

有人告诉我这个被称为" 延迟反模式 "或" Promise构造函数反模式 ",这个代码有什么不好,为什么这被称为 …

javascript promise q bluebird es6-promise

479
推荐指数
3
解决办法
7万
查看次数

为什么不能将Promise.resolve作为函数调用?

有些东西让我和我的同事烦恼.考虑以下...

const {map, compose} = require('ramda');

compose(
  console.log,
  map(Math.tan)
)([1,2,3]);

compose(
  console.log,
  map(v=>Promise.resolve(v))
)([4,5,6]);

compose(
  console.log,
  map(Promise.resolve)
)([7,8,9]);
Run Code Online (Sandbox Code Playgroud)

正如您所期望的那样,输出1,2和3的棕褐色,以及解决3,4和5的承诺也是如此.但我的问题是......为什么第三次突破?为什么Promise.resolve的行为与其他函数的行为方式不同?

[ 1.5574077246549023, -2.185039863261519, -0.1425465430742778 ]
[ Promise { 4 }, Promise { 5 }, Promise { 6 } ]
/home/xxx/node_modules/ramda/src/internal/_map.js:6
    result[idx] = fn(functor[idx]);
                  ^

TypeError: PromiseResolve called on non-object
    at resolve (<anonymous>)
    at _map (/home/xxx/node_modules/ramda/src/internal/_map.js:6:19)
    at map (/home/xxx/node_modules/ramda/src/map.js:57:14)
    at /home/xxx/node_modules/ramda/src/internal/_dispatchable.js:39:15
    at /home/xxx/node_modules/ramda/src/internal/_curry2.js:20:46
    at f1 (/home/xxx/node_modules/ramda/src/internal/_curry1.js:17:17)
    at /home/xxx/node_modules/ramda/src/internal/_pipe.js:3:27
    at /home/xxx/node_modules/ramda/src/internal/_arity.js:5:45
    at Object.<anonymous> (/home/xxx/b.js:20:6)
    at Module._compile (module.js:569:30)
Run Code Online (Sandbox Code Playgroud)

javascript functional-programming promise ramda.js

5
推荐指数
1
解决办法
1628
查看次数

是".then(function(a){return a;})"对于承诺的无操作?

我正在阅读有关Bookshelf的本教程.Bookshelf使用Bluebird承诺.有很多例子看起来像这样:

var getEvents = function(participantId) {  
  return new models.Participant()
    .query({where: {id: participantId}})
    .fetch({withRelated: ['events'], require: true})
    .then(function(model) {
      return model;
    });
};
Run Code Online (Sandbox Code Playgroud)

我对承诺仍然不满意,但从我到目前为止所学到的东西看起来很奇怪.我的问题是,上述功能是否与fetch()直接返回并离开最终版本完全相同then():

var getEvents = function(participantId) {  
  return new models.Participant()
    .query({where: {id: participantId}})
    .fetch({withRelated: ['events'], require: true});
};
Run Code Online (Sandbox Code Playgroud)

也就是说,它仍然做同样的事情,返回相同的承诺,可以以相同的方式调用,等等?

根据我的理解,传递给函数的参数then获取链中前一个promise的返回值.所以,在我看来.then(function (a) { return a; }),一般来说只是一个无操作.对?

如果它们不一样,有什么区别?发生了什么,为什么作者这样写呢?

javascript node.js promise bluebird

4
推荐指数
2
解决办法
1689
查看次数