Why I can't pass console.log as a callback argument in Chrome (and Safari)?

后端 未结 2 1091
夕颜
夕颜 2021-01-17 22:25

The following snippet will produce an error in Chrome (and Safari) while it works in Firefox.

I\'d expect to have 2 numbers shown in javascript console, but in Chrom

2条回答
  •  终归单人心
    2021-01-17 23:08

    Try utilizing deferred.resolveWith()

    // a generic promise that return a random float
    var makePromise = function() {
      // set `this` to `window.console` , 
      // pass arguments within array
      return $.Deferred().resolveWith(window.console, [Math.random()]);
    }
    
    // This works in all browsers
    makePromise().then(console.log)
    //.then(function(d) {
    //  console.log(d);
    //});
    // This works in firefox only
    makePromise().then(console.log);

提交回复
热议问题