在javascript中使用范围之外的数据

时间:2016-07-23 16:43:58

标签: javascript promise

我在JavaScript中获得以下任务:使用名为getData的方法创建一个DataBase对象,该方法返回一个promise。解决此承诺后,它将返回一个数组。

创建第二个名为Data的对象,该对象调用getData方法。为Data创建一个名为getFirst的方法,它返回数组中第一个成员的值。

如果我理解这项任务,那是不可能的,因为你不能在回调之外的诺言中分配价值。

(实际上,任务更复杂,但我正在简化以显示我遇到问题的部分。)

以下是我所拥有的:

java.lang.NoSuchMethodError: android.graphics.drawable.VectorDrawable_Delegate.nCreateTreeFromCopy(JJ)J
at android.graphics.drawable.VectorDrawable.nCreateTreeFromCopy(VectorDrawable.java)
at android.graphics.drawable.VectorDrawable.access$400(VectorDrawable.java:204)
at android.graphics.drawable.VectorDrawable$VectorDrawableState.createNativeTreeFromCopy(VectorDrawable.java:837)
at android.graphics.drawable.VectorDrawable$VectorDrawableState.<init>(VectorDrawable.java:812)
at android.graphics.drawable.VectorDrawable.mutate(VectorDrawable.java:268)
at android.graphics.drawable.DrawableContainer$DrawableContainerState.addChild(DrawableContainer.java:817)
at android.graphics.drawable.StateListDrawable$StateListState.addStateSet(StateListDrawable.java:329)
at android.graphics.drawable.AnimatedStateListDrawable$AnimatedStateListState.addStateSet(AnimatedStateListDrawable.java:602)
at android.graphics.drawable.AnimatedStateListDrawable.parseItem(AnimatedStateListDrawable.java:525)
at android.graphics.drawable.AnimatedStateListDrawable.inflateChildElements(AnimatedStateListDrawable.java:453)
at android.graphics.drawable.AnimatedStateListDrawable.inflate(AnimatedStateListDrawable.java:385)
at android.graphics.drawable.DrawableInflater.inflateFromXml(DrawableInflater.java:130)
at android.graphics.drawable.Drawable.createFromXmlInner(Drawable.java:1224)
at android.graphics.drawable.Drawable.createFromXml(Drawable.java:1197)
at com.android.layoutlib.bridge.impl.ResourceHelper.getDrawable(ResourceHelper.java:315)
at android.content.res.BridgeTypedArray.getDrawable(BridgeTypedArray.java:681)
at android.widget.CompoundButton.<init>(CompoundButton.java:89)
at android.widget.CheckBox.<init>(CheckBox.java:70)
at android.widget.CheckBox.<init>(CheckBox.java:66)
at android.widget.CheckBox.<init>(CheckBox.java:62)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.view.LayoutInflater.createView(LayoutInflater.java:645)
at android.view.BridgeInflater.onCreateView(BridgeInflater.java:107)
at android.view.LayoutInflater.onCreateView(LayoutInflater.java:717)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:785)
at android.view.BridgeInflater.createViewFromTag(BridgeInflater.java:149)
at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
at android.view.LayoutInflater.rInflate_Original(LayoutInflater.java:858)
at android.view.LayoutInflater_Delegate.rInflate(LayoutInflater_Delegate.java:70)
at android.view.LayoutInflater.rInflate(LayoutInflater.java:834)
at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
at com.android.layoutlib.bridge.impl.RenderSessionImpl.inflate(RenderSessionImpl.java:317)
at com.android.layoutlib.bridge.Bridge.createSession(Bridge.java:429)
at com.android.ide.common.rendering.LayoutLibrary.createSession(LayoutLibrary.java:350)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:520)
at com.android.tools.idea.rendering.RenderTask$2.compute(RenderTask.java:508)
at com.intellij.openapi.application.impl.ApplicationImpl.runReadAction(ApplicationImpl.java:967)
at com.android.tools.idea.rendering.RenderTask.createRenderSession(RenderTask.java:508)
at com.android.tools.idea.rendering.RenderTask.access$600(RenderTask.java:75)
at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:620)
at com.android.tools.idea.rendering.RenderTask$3.call(RenderTask.java:617)
at com.android.tools.idea.rendering.RenderService.runRenderAction(RenderService.java:371)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:617)
at com.android.tools.idea.rendering.RenderTask.render(RenderTask.java:639)
at com.intellij.android.designer.designSurface.AndroidDesignerEditorPanel$7.run(AndroidDesignerEditorPanel.java:519)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:337)
at com.intellij.util.ui.update.MergingUpdateQueue.execute(MergingUpdateQueue.java:327)
at com.intellij.util.ui.update.MergingUpdateQueue$3.run(MergingUpdateQueue.java:271)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:286)
at com.intellij.util.ui.update.MergingUpdateQueue.flush(MergingUpdateQueue.java:244)
at com.intellij.util.ui.update.MergingUpdateQueue.run(MergingUpdateQueue.java:234)
at com.intellij.util.concurrency.QueueProcessor.runSafely(QueueProcessor.java:238)
at com.intellij.util.Alarm$Request$1.run(Alarm.java:352)
at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511)
at java.util.concurrent.FutureTask.run(FutureTask.java:266)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at java.lang.Thread.run(Thread.java:745)

2 个答案:

答案 0 :(得分:2)

initialize函数上使用promise并从initialze函数中返回一个promise:

this.initialize = function(){
    dataBase = new DataBase();
    return dataBase.getData().then( function(theArray){   
        console.log(theArray); //works fine
        that.first = theArray[0]; //doesn't work because promise is asyncronous
    })
}    

d.initialize().then(function() {
    result = d.getFirst();
    console.log(result);
});

答案 1 :(得分:1)

您的理解是正确的。

  

使用名为getData的方法创建一个DataBase对象,该方法返回一个promise。解决此承诺后,它将返回一个数组。

     

创建一个名为Data的第二个方法,该方法调用getData方法。为Data创建一个名为getFirst的方法,它返回数组中第一个成员的值。

这是非常草率的语言。承诺不会返回某些东西,他们履行并带来一些价值。并且getFirst肯定不会返回第一个成员本身,但会为其返回承诺

此外还不清楚Data是否应该是构造函数 / class,因为如果是这样的话,它不应该被称为方法。或者,如果不是,那么&#34;数据的方法&#34;没有意义。

我会理解这项任务,以便预期结果只是

var DataBase = {
    getData: function() {
        var array = […];
        return Promise.resolve(array);
    },
    getFirst: function() {
        return this.getData().then(function(array) { return array[0]; });
    }
};