首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Geofire中使用promises

在Geofire中使用promises
EN

Stack Overflow用户
提问于 2017-08-23 21:02:39
回答 1查看 311关注 0票数 1

Geofire文档说Geofire在读写数据时会返回承诺,但我似乎找不到任何这样的例子。我想知道如何在javascript中使用它来将一个函数附加到监听程序的末尾,该监听程序在geofire承诺实现时触发。我的代码是:

代码语言:javascript
复制
var onKeyEnteredRegistration = this.geoQuery.on("key_entered", function(key, location, distance) {
    console.log(key + " entered query at " + location + " (" + distance + " km from center)");
    }).then(function(result){
          console.log("promise resolved with:" + result);
        }, function(error){
          console.error(error)
        });

但是这会输出一个错误,说'undefined is not a function‘引用了链接的函数。有人知道如何在Geofire中使用promises吗?

谢谢

EN

回答 1

Stack Overflow用户

发布于 2018-07-26 04:31:44

因此,只有GeoFire类上的getsetremove函数返回promises。如下所示:

代码语言:javascript
复制
geoFire.get("some_key").then(function(location) {
  if (location === null) {
    console.log("Provided key is not in GeoFire");
  }
  else {
    console.log("Provided key has a location of " + location);
  }
}, function(error) {
  console.log("Error: " + error);
});

但是,on函数返回一个GeoQuery,它的on函数返回一个GeoCallbackRegistration,用于终止cancel函数的查询(该函数不返回任何内容)。

代码语言:javascript
复制
var onKeyEnteredRegistration = this.geoQuery.on("key_entered", function(key, location, distance) {
     console.log(key + " entered query at " + location + " (" + distance + " km from center)");
});

onKeyEnteredRegistration.cancel(); // the "key_entered" event will stop firing
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45840546

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档