The key thing is that token ATA is not fetched from chain, it is calculated, so it can be gotten even if it doesn't exist yet.
Yes you absolutely can, the order of instruction is what matters here. This is what happens when you send any token from your wallet to a new address. First create ATA instruction is passed and then the transfer instruction. The 'associatedToken' variable in this case will be passed in destination. Here's an example code:
const token = new PublicKey(mint);
const transaction = new Transaction();
const fromTokenAccount = await getAssociatedTokenAddress(token, fromPubKey);
const toTokenAccount = await getAssociatedTokenAddress(token, toPubKey);
const toTokenAccountInfo = await connection.getAccountInfo(toTokenAccount);
// Add create token account instruction if source account not created
if (!toTokenAccountInfo) {
transaction.add(
createAssociatedTokenAccountInstruction(
fromPubKey,
toTokenAccount,
toPubKey,
token
)
);
}
// Add token transfer instructions to transaction
transaction.add(
createTransferInstruction(
fromTokenAccount,
toTokenAccount,
fromPubKey,
Number(amount) * 1000000 // Token decimal (6 in case of USDC)
)
);
The key thing is that token ATA is not fetched from chain, it is calculated, so it can be gotten even if it doesn't exist yet.