DevPrep

All topics › Networking & APIs

How do you make a synchronous wrapper around an async method?

Middle PRO

Essence

Technically — with a DispatchSemaphore: create a semaphore with value 0, call the asynchronous method, store the result and signal() in the callback, and block on wait() outside. But in an interview it matters more to explain why this is an anti-pattern and what the correct direction of wrapping is.

The rest of this answer, with code and interview follow-ups, is in the DevPrep app.

Read the full answer in DevPrep

Practice this before your interview

DevPrep turns these answers into one-minute challenges: what does this code print, find the bug, mock interviews. One challenge a day.

Get DevPrep on the App Store