Mixing async/await with Result

前端 未结 2 472
一个人的身影
一个人的身影 2021-02-14 22:43

Let me just preface this question with a few things:

  1. I\'ve read several SO questions saying that you should not do this (such as How to safely mix
2条回答
  •  难免孤独
    2021-02-14 23:44

    Scenario one: you are sitting at your desk. There is an inbox. It is empty. A piece of paper suddenly arrives in your inbox describing a task. You jump to your feet and start running around doing the task. But what is the task? It says to do the following:

    • Change the whiteboard to say "running" -- OK, you do that.
    • Set your alarm clock for an hour later. OK, you do that.
    • Create a new piece of paper that says "when the alarm goes off, write the word DONE on the whiteboard". Put it in your inbox. You do that.
    • Do nothing else until the word DONE is written on the whiteboard.
    • Go back to your desk and wait for the next task to arrive in the inbox.

    This workflow prevents you from getting work done because the last two steps are in the wrong order.

    Scenario two: you are sitting at your desk. There is an inbox. It is empty. A piece of paper suddenly arrives in your inbox describing a task. You jump to your feet and start running around doing the task. But what is the task? It says to do the following:

    • Change the whiteboard to say "running" -- OK, you do that.
    • Give this other piece of paper to Debbie in the next cubicle. OK, you do that.
    • Do nothing until someone tells you that the sub-task is DONE.
    • When that happens, write the word DONE on your whiteboard.
    • Go back to your desk.

    What does the piece of paper you gave Debbie say? It says:

    • Set your alarm clock for an hour later. OK, she does that.
    • When the alarm goes off, put a piece of paper in your inbox saying to tell Middas that you're done.

    This workflow still is terrible in that (1) you are sitting there doing nothing while you wait for Debbie's alarm clock to go off, and (2) you are wasting the time of two workers when you could have a single worker do all the work. Workers are expensive.

    But this workflow does not prevent you from getting work done eventually. It doesn't deadlock because you are not waiting on work that you yourself are going to do in the future, you are waiting for someone else to do the work.

    (I note that this is not an exact analogy for what is happening in your program, but it is close enough to get the idea across.)

提交回复
热议问题