我有一个返回承诺的简单服务。但是,如果它是基元,它只触发then()。我无法想象我的生活。任何帮助将不胜感激。我意识到这可能是一件简单的事情,但我已经查看了许多例子,并且无法弄清楚为什么它不能从我的服务中返回我的对象。
02-12 12:29:03.728 8994-8994/? I/zygote: Not late-enabling -Xcheck:jni (already on)
02-12 12:29:03.784 8994-8994/? W/zygote: Unexpected CPU variant for X86 using defaults: x86
02-12 12:29:03.856 8994-9002/? E/zygote: Failed writing handshake bytes (-1 of 14): Broken pipe
02-12 12:29:03.856 8994-9002/? I/zygote: Debugger is no longer active
02-12 12:29:03.878 8994-9002/? W/zygote: Suspending all threads took: 22.488ms
02-12 12:29:03.944 8994-8994/? I/InstantRun: starting instant run server: is main process
02-12 12:29:04.303 8994-9016/? D/OpenGLRenderer: HWUI GL Pipeline
[ 02-12 12:29:04.401 8994: 9016 D/ ]
HostConnection::get() New Host Connection established 0x9a926e80, tid 9016
02-12 12:29:04.459 8994-8999/? I/zygote: Do partial code cache collection, code=9KB, data=18KB
02-12 12:29:04.459 8994-8999/? I/zygote: After code cache collection, code=9KB, data=18KB
02-12 12:29:04.459 8994-8999/? I/zygote: Increasing code cache capacity to 128KB
02-12 12:29:04.460 8994-8999/? I/zygote: Do partial code cache collection, code=9KB, data=36KB
02-12 12:29:04.460 8994-8999/? I/zygote: After code cache collection, code=9KB, data=36KB
02-12 12:29:04.460 8994-8999/? I/zygote: Increasing code cache capacity to 256KB
02-12 12:29:04.460 8994-8999/? I/zygote: JIT allocated 71KB for compiled code of void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
02-12 12:29:04.460 8994-8999/? I/zygote: Compiler allocated 4MB to compile void android.widget.TextView.<init>(android.content.Context, android.util.AttributeSet, int, int)
02-12 12:29:04.595 8994-9016/? I/zygote: android::hardware::configstore::V1_0::ISurfaceFlingerConfigs::hasWideColorDisplay retrieved: 0
02-12 12:29:04.595 8994-9016/? I/OpenGLRenderer: Initialized EGL, version 1.4
02-12 12:29:04.595 8994-9016/? D/OpenGLRenderer: Swap behavior 1
02-12 12:29:04.714 8994-9016/? D/EGL_emulation: eglCreateContext: 0xa50e6160: maj 2 min 0 rcv 2
02-12 12:29:04.753 8994-9016/? D/EGL_emulation: eglMakeCurrent: 0xa50e6160: ver 2 0 (tinfo 0xa501b6f0)
02-12 12:29:04.863 8994-9016/com.example.sparsh.newcalculator D/EGL_emulation: eglMakeCurrent: 0xa50e6160: ver 2 0 (tinfo 0xa501b6f0)
02-12 12:29:07.446 8994-8994/com.example.sparsh.newcalculator I/Choreographer: Skipped 174 frames! The application may be doing too much work on its main thread.
02-12 12:29:12.710 8994-8994/com.example.sparsh.newcalculator D/AndroidRuntime: Shutting down VM
02-12 12:29:12.711 8994-8994/com.example.sparsh.newcalculator E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.sparsh.newcalculator, PID: 8994
java.lang.NumberFormatException: For input string: " 666"
at java.lang.Integer.parseInt(Integer.java:597)
at java.lang.Integer.parseInt(Integer.java:643)
at com.example.sparsh.newcalculator.MainActivity$11.onClick(MainActivity.java:147)
at android.view.View.performClick(View.java:6294)
at android.view.View$PerformClick.run(View.java:24770)
at android.os.Handler.handleCallback(Handler.java:790)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6494)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
修改
它似乎与类型有关。如果我使用Point,它可以正常工作。
//CreateLayer inside IncidentLayer class
public CreateLayer(): Promise<any>{
return this.incidentService.getIncidents().toPromise().then((incs) => {
var gfx = this.createGraphics(incs);//not an async function
var lyr = new this._mapDependencies.FeatureLayer({
source: gfx
});
//return lyr;WILL NOT TRIGGER
return "test";//WORKS
});
}
......
this.incidentLayer.CreateLayer().then((lyr)=>{
console.log(lyr);//only triggers if lyr is a primitive
});
我在控制台或添加catch()之后没有错误,所以我不确定如何调试它。
_mapDependencies通过以下esri角度加载器库创建。
var lyr = new this._mapDependencies.Point();
return lyr;//works
答案 0 :(得分:0)
您的CreateLayer()函数不会返回承诺!您无法解决承诺并将其作为承诺返回。使用RxJs obserables会更容易,但承诺的解决方案应如下所示:
public CreateLayer(): Promise<any>{
return new Promise(resolve=>{
this.incidentService.getIncidents().toPromise().then((incs) => {
var gfx = this.createGraphics(incs);//not an async function
var lyr = new this._mapDependencies.FeatureLayer({
source: gfx
});
resolve(lyr);
})
});
}
我用另一个承诺包装你的getIncidents(),这是将回复给将调用此函数的任何人的承诺。当它被解析时,调用函数.then()回调将是
你应该多读一些关于承诺如何运作here
的内容