我正在编写一个蒸汽机器人,并试图检查某笔交易中某项物品的价值是否低于1美元。我需要访问“ pass”变量,但是它永远无法正常工作。有人可以帮忙吗?
public class MainActivity extends AppCompatActivity {
final Top_Title_Fragment top_title_fragment = new Top_Title_Fragment();
final Bereidingstijd_Text bereidingstijd_text = new Bereidingstijd_Text();
final Waardering_Text waardering_text = new Waardering_Text();
final Calorieen_Text calorieen_text = new Calorieen_Text();
final Vaardigheid_Text vaardigheid_text = new Vaardigheid_Text();
final Kruiden_Text kruiden_text = new Kruiden_Text();
final Bereidingstijd_Value bereidingstijd_value = new Bereidingstijd_Value();
final Waardering_Value waardering_value = new Waardering_Value();
final Calorieen_Value calorieen_value = new Calorieen_Value();
final Vaardigheid_Value vaardigheid_value = new Vaardigheid_Value();
final Kruiden_Value kruiden_value = new Kruiden_Value();
final Picture_Bowl_Fragment picture_bowl_fragment = new Picture_Bowl_Fragment();
final Picture_DuckFull_Fragment picture_duckFull_fragment = new Picture_DuckFull_Fragment();
final Picture_DuckPlate_Fragment picture_duckPlate_fragment = new Picture_DuckPlate_Fragment();
final Bereiding_Button_Fragment bereiding_button_fragment = new Bereiding_Button_Fragment();
final Ingredienten_Button_Fragment ingredienten_button_fragment = new Ingredienten_Button_Fragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction()
.setCustomAnimations(R.anim.slide_right, R.anim.slide_right)
.add(R.id.container, bereidingstijd_text)
.add(R.id.container, waardering_text)
.add(R.id.container, calorieen_text)
.add(R.id.container, vaardigheid_text)
.add(R.id.container, kruiden_text)
.setCustomAnimations(R.anim.slide_left, R.anim.slide_left)
.add(R.id.container, bereidingstijd_value)
.add(R.id.container, kruiden_value)
.add(R.id.container, waardering_value)
.add(R.id.container, calorieen_value)
.add(R.id.container, vaardigheid_value)
.setCustomAnimations(R.anim.slide_down, R.anim.slide_down)
.add(R.id.container, bereiding_button_fragment)
.add(R.id.container, ingredienten_button_fragment)
.setCustomAnimations(R.anim.slide_down_title, R.anim.slide_down_title)
.add(R.id.container, top_title_fragment)
.setCustomAnimations(R.anim.mid_pic_anim, R.anim.mid_pic_anim)
.add(R.id.container, picture_bowl_fragment)
.setCustomAnimations(R.anim.right_pic_anim, R.anim.slide_left)
.add(R.id.container, picture_duckFull_fragment)
.setCustomAnimations(R.anim.left_pic_anim, R.anim.slide_right)
.add(R.id.container, picture_duckPlate_fragment)
.addToBackStack(null)
.commit();
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
getSupportFragmentManager().beginTransaction()
.remove(bereidingstijd_text)
.remove(bereidingstijd_value)
.setCustomAnimations(R.anim.zoom_in_text, R.anim.zoom_in_text)
.add(R.id.container, bereidingstijd_text)
.add(R.id.container, bereidingstijd_value)
.addToBackStack(null)
.commit();
final Handler handler1 = new Handler();
handler1.postDelayed(new Runnable() {
@Override
public void run() {
getSupportFragmentManager().beginTransaction()
.remove(bereidingstijd_text)
.remove(bereidingstijd_value)
.remove(waardering_text)
.remove(waardering_value)
.add(R.id.container, bereidingstijd_text)
.add(R.id.container, bereidingstijd_value)
.setCustomAnimations(R.anim.zoom_in_text, R.anim.zoom_in_text)
.add(R.id.container, waardering_text)
.add(R.id.container, waardering_value)
.addToBackStack(null)
.commit();
final Handler handler2 = new Handler();
handler2.postDelayed(new Runnable() {
@Override
public void run() {
getSupportFragmentManager().beginTransaction()
.remove(waardering_text)
.remove(waardering_value)
.remove(calorieen_text)
.remove(calorieen_value)
.add(R.id.container, waardering_text)
.add(R.id.container, waardering_value)
.setCustomAnimations(R.anim.zoom_in_text, R.anim.zoom_in_text)
.add(R.id.container, calorieen_text)
.add(R.id.container, calorieen_value)
.addToBackStack(null)
.commit();
final Handler handler3 = new Handler();
handler3.postDelayed(new Runnable() {
@Override
public void run() {
getSupportFragmentManager().beginTransaction()
.remove(calorieen_text)
.remove(calorieen_value)
.remove(vaardigheid_text)
.remove(vaardigheid_value)
.add(R.id.container, calorieen_text)
.add(R.id.container, calorieen_value)
.setCustomAnimations(R.anim.zoom_in_text, R.anim.zoom_in_text)
.add(R.id.container, vaardigheid_text)
.add(R.id.container, vaardigheid_value)
.addToBackStack(null)
.commit();
final Handler handler4 = new Handler();
handler4.postDelayed(new Runnable() {
@Override
public void run() {
getSupportFragmentManager().beginTransaction()
.remove(vaardigheid_text)
.remove(vaardigheid_value)
.remove(kruiden_text)
.remove(kruiden_value)
.add(R.id.container, vaardigheid_text)
.add(R.id.container, vaardigheid_value)
.setCustomAnimations(R.anim.zoom_in_text, R.anim.zoom_in_text)
.add(R.id.container, kruiden_text)
.add(R.id.container, kruiden_value)
.addToBackStack(null)
.commit();
}
}, 3000L);
}
}, 3000L);
}
}, 3000L);
}
}, 3000L);
}
},3000L);
}
}
答案 0 :(得分:1)
这个问题经常被问到,但是希望以您自己问题的术语来解释它会帮助您理解。
您的代码应该等待pass = false;
的任何可能性,但这不是因为您调用了异步函数---假设代码在调用之后立即继续运行。您的foreach函数将立即处理,直到.getItemPrice
稍后响应之前,将调用下一行。
因此,要“等待”所有结果,您可以执行以下操作:
var receive = offer.itemsToReceive;
var pass = true;
var itemReceivePromises = receive.map(id=>{
var args = id.split(",");
return market.getItemPrice("some app id", some item name).then(function(result) {
var json = JSON.parse(JSON.stringify(result)); // returns e.G $0.08
var price = parseFloat(json.median_price.substring(1));
if(price*100 < 100) {
pass = false;
}
});
});
Promise.all(itemRecievePromises).then(results=>{
console.log('pass',pass);
});
但是,您应该将“通过”结果传递回promise,而不要使用更高范围的变量。